Geting Article id after ContentModelArticle->Save($data)

General questions relating to Joomla! 2.5. Note: All 1.6 and 1.7 releases have reached end of life and should be updated to 2.5. There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
damianocastro
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Mon Nov 05, 2012 11:13 am

Geting Article id after ContentModelArticle->Save($data)

Postby damianocastro » Thu Jun 11, 2015 8:17 pm

Hello all,
I want to retrieve the id of the article that I've just created with ContentModelArticle->Save($data). Is there any way to do this?

Thanks!

itoctopus
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4025
Joined: Mon Nov 25, 2013 4:35 pm
Location: Montreal, Canada
Contact:

Re: Geting Article id after ContentModelArticle->Save($data)

Postby itoctopus » Tue Oct 06, 2015 11:22 am

Not with this class (as far as I know), but you can always query MySQL for the last inserted id in that table.
http://www.itoctopus.com - Joomla consulting at its finest
https://twitter.com/itoctopus - Follow us on Twitter

User avatar
AndyGaskell
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Fri Oct 31, 2008 3:12 pm
Location: Aberdeen
Contact:

Re: Geting Article id after ContentModelArticle->Save($data)

Postby AndyGaskell » Thu May 03, 2018 10:59 am

I found that...

Code: Select all

        $item = $new_article->getItem();
        $id = $item->id;

...worked ok.

So, like...

Code: Select all

        require_once (JPATH_ADMINISTRATOR.'/components/com_content/models/article.php');
        $new_article = new ContentModelArticle();
        $data = array(
            "catid"=>39, # draft apps
            "title"=>"title here",
        );
        $new_article->save($data);
        $item = $new_article->getItem();
        $id = $item->id;       
My work: http://www.ssofb.co.uk.
Twitter: https://twitter.com/AndyGasman.
I'm a programmer at Software Systems: Open For Business in Aberdeen Scotland doing LAMP web development.


Return to “General Questions/New to Joomla! 2.5”

Who is online

Users browsing this forum: No registered users and 8 guests