How to get $id immediately after jtable->store

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
yoda42
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Sat Jan 24, 2009 9:53 am
Location: Johannesburg, South Africa

How to get $id immediately after jtable->store

Post by yoda42 » Tue Jan 27, 2009 4:51 pm

I am working on one of my first MVC components and making good progress, but need help getting the ID of a new record I have just stored using JTable.

I have an HTML form in a view where a user can capture a series of information. when the user hits submit, I call a task in the controller. In this task I manipulate the POST data and store it. Once stored, the controller displays a second view. Before displaying the second view I need to pass the view the id of the record I have just been working on.

In the case where I am updating a record, this is easily available as $id is a variable available in POST. In the case of a new record, I do not have access to this info, and I cannot find a method in JTABLE to retrieve the id of the record.

Anyone with some suggestions ?
Yoda

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: How to get $id immediately after jtable->store

Post by dam-man » Tue Jan 27, 2009 8:22 pm

I need this to, couldn't find it also..
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to get $id immediately after jtable->store

Post by ianmac » Tue Jan 27, 2009 9:18 pm

yoda42 wrote:I am working on one of my first MVC components and making good progress, but need help getting the ID of a new record I have just stored using JTable.

I have an HTML form in a view where a user can capture a series of information. when the user hits submit, I call a task in the controller. In this task I manipulate the POST data and store it. Once stored, the controller displays a second view. Before displaying the second view I need to pass the view the id of the record I have just been working on.

In the case where I am updating a record, this is easily available as $id is a variable available in POST. In the case of a new record, I do not have access to this info, and I cannot find a method in JTABLE to retrieve the id of the record.

Anyone with some suggestions ?
Yoda
Just do:
$row->store();
echo $row->id;

i.e. The id is automatically updated after the row is stored.

Ian

yoda42
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Sat Jan 24, 2009 9:53 am
Location: Johannesburg, South Africa

Re: How to get $id immediately after jtable->store

Post by yoda42 » Thu Jan 29, 2009 10:34 pm

Hi Ian

Thanks a ton. That definitely did it!

That should have been semi-obvious.

peachio
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Nov 15, 2010 11:08 am

Re: How to get $id immediately after jtable->store

Post by peachio » Tue Jan 11, 2011 11:05 am

Code: Select all

/**
	 * save a record
	 * @return void
	 */
	public function apply(){
		$model = $this->getModel('sections'); 

		if ($model->store()) {
			$msg = JText::_( 'Data Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Data' );
		}
       
        $lastinsertid = $model->id;
		
		$link = 'index.php?option=com_contactxt&controller=sectionslist&task=edit&cid[]='.$lastinsertid;
		$this->setRedirect($link, $msg);
	}
cannot workout why this is not working, can anyone analyse when i give more details...
got no idea at the moment where the bug is.

yoda42
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Sat Jan 24, 2009 9:53 am
Location: Johannesburg, South Africa

Re: How to get $id immediately after jtable->store

Post by yoda42 » Tue Jan 11, 2011 12:40 pm

Can you post the error message ?

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to get $id immediately after jtable->store

Post by ianmac » Tue Jan 11, 2011 1:58 pm

peachio wrote:

Code: Select all

/**
	 * save a record
	 * @return void
	 */
	public function apply(){
		$model = $this->getModel('sections'); 

		if ($model->store()) {
			$msg = JText::_( 'Data Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Data' );
		}
       
        $lastinsertid = $model->id;
		
		$link = 'index.php?option=com_contactxt&controller=sectionslist&task=edit&cid[]='.$lastinsertid;
		$this->setRedirect($link, $msg);
	}
cannot workout why this is not working, can anyone analyse when i give more details...
got no idea at the moment where the bug is.
Your model probably doesn't have a property called id. You would have to look at your model's store method to see if it does anything with the id and/or if there is a method to expose it to the controller (which I presume is where the code you posted is from). Then you should call that method to get the id.

Regards

peachio
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Nov 15, 2010 11:08 am

Re: How to get $id immediately after jtable->store

Post by peachio » Tue Jan 11, 2011 5:48 pm

hi
as far is i know in the jtable model theres a property $id equals null, in the header of the jtable model.
the constructor is something like
function __construct(&$db)
{
parent::__construct( '#__contactxt', 'id', $db );
}
}
thats all i remember from home...should i call a getid $this $id equals $id if i understood you correctly.
greetings peachio

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to get $id immediately after jtable->store

Post by ianmac » Tue Jan 11, 2011 5:55 pm

peachio wrote:hi
as far is i know in the jtable model theres a property $id equals null, in the header of the jtable model.
the constructor is something like
function __construct(&$db)
{
parent::__construct( '#__contactxt', 'id', $db );
}
}
thats all i remember from home...should i call a getid $this $id equals $id if i understood you correctly.
greetings peachio
What happens if you do:

echo get_class($model);

Is it a JTable object?

Regards

peachio
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Nov 15, 2010 11:08 am

Re: How to get $id immediately after jtable->store

Post by peachio » Tue Jan 11, 2011 6:08 pm

if i remember right i get the message data saved, means that the values get written to the db then incorrect redirection because of missing lastupdate id
im already back home and cannot access my computer at work means i will check tomorrow moring. thank you for the tip..greetings peachio

peachio
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Nov 15, 2010 11:08 am

Re: How to get $id immediately after jtable->store

Post by peachio » Wed Jan 12, 2011 7:50 am

preasumtion that JTable model gets loaded straight away:-/ In fact the common model overloads the store method then JTable is used from this model, so I added a class var $_id= null to the model and a getId function with return $this->_id. cheers ianmac!
peachio

peachio
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Nov 15, 2010 11:08 am

Re: How to get $id immediately after jtable->store

Post by peachio » Wed Jan 12, 2011 7:56 am

ContactxtModelCategories was the class name...
in the model when store is called the id must be set again.

$this->setId($row->id); //resp. $model->setId

User avatar
sakiss
Joomla! Explorer
Joomla! Explorer
Posts: 252
Joined: Wed Aug 20, 2008 4:09 pm

Re: How to get $id immediately after jtable->store

Post by sakiss » Wed Jul 10, 2013 7:03 pm

Just an update to this in order to work with J2.5

You should use the primary key's name in order to work.

For example:

Code: Select all

$row->product_id


Locked

Return to “Joomla! 1.5 Coding”