0

I work on a custom Joomla module that creates articles programmatically. I use the below code to create the article. It all works fine. Now I would need to get back the ID of the newly create article so that I can store it for future use. I checked the bind, check, store functions, but none of them seem to return the ID. How can I do this?

Thanks! W.

static function createArticle(GNGLocation $loc, GNGLang $lang, $title, $alias, $intro, $summary, $imageStr) {

        $table = JTable::getInstance('Content', 'JTable', array());

        $jcat = $loc->get('jcat_id_tour');
        $jlang = $lang->get('joomlaName');

        $data = array(
            'catid' => $jcat,
            'title' => $title,
            'alias' =>  $alias,
            'language' => $jlang,
            'introtext' => $summary,
            'fulltext' => $intro,
            'state' => 0,
        );

        // Bind data
        if (!$table->bind($data))
        {
            throw new Exception("Failed to bind article data. Error: " . $table->getError());
        }

        // Check the data.
        if (!$table->check())
        {
            throw new Exception("Failed to check article data. Error: " . $table->getError());
        }

        // Store the data.
        if (!$table->store())
        {
            throw new Exception("Failed to store article data. Error: " . $table->getError());
        }

    } 
  • Is it not $table->id ? Did you var_dump($table) after calling the store() method? How about $table->insertId()? – mickmackusa Dec 2 '19 at 21:16
  • If the solution is here: joomla.stackexchange.com/q/10627/12352 then we'll need to close this one rather than answering. – mickmackusa Dec 3 '19 at 0:09
  • 1
    @mickmackusa: thanks for the hints var_dump($table->id) did the trick (called after store() ) Thanks! – wanderlusted Dec 3 '19 at 11:13

Browse other questions tagged or ask your own question.