Page 1 of 1

How to get $id immediately after jtable->store

Posted: Tue Jan 27, 2009 4:51 pm
by yoda42
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

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

Posted: Tue Jan 27, 2009 8:22 pm
by dam-man
I need this to, couldn't find it also..

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

Posted: Tue Jan 27, 2009 9:18 pm
by ianmac
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

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

Posted: Thu Jan 29, 2009 10:34 pm
by yoda42
Hi Ian

Thanks a ton. That definitely did it!

That should have been semi-obvious.

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

Posted: Tue Jan 11, 2011 11:05 am
by peachio

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.

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

Posted: Tue Jan 11, 2011 12:40 pm
by yoda42
Can you post the error message ?

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

Posted: Tue Jan 11, 2011 1:58 pm
by ianmac
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

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

Posted: Tue Jan 11, 2011 5:48 pm
by peachio
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

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

Posted: Tue Jan 11, 2011 5:55 pm
by ianmac
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

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

Posted: Tue Jan 11, 2011 6:08 pm
by peachio
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

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

Posted: Wed Jan 12, 2011 7:50 am
by peachio
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

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

Posted: Wed Jan 12, 2011 7:56 am
by peachio
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

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

Posted: Wed Jul 10, 2013 7:03 pm
by sakiss
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