How to get $id immediately after jtable->store
-
- Joomla! Apprentice
- Posts: 11
- Joined: Sat Jan 24, 2009 9:53 am
- Location: Johannesburg, South Africa
How to get $id immediately after jtable->store
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
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
- dam-man
- 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
I need this to, couldn't find it also..
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards
Dutch Boards | Joomla Coding Boards | English Support Boards
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: How to get $id immediately after jtable->store
Just do: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
$row->store();
echo $row->id;
i.e. The id is automatically updated after the row is stored.
Ian
-
- 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
Hi Ian
Thanks a ton. That definitely did it!
That should have been semi-obvious.
Thanks a ton. That definitely did it!
That should have been semi-obvious.
-
- Joomla! Apprentice
- Posts: 16
- Joined: Mon Nov 15, 2010 11:08 am
Re: How to get $id immediately after jtable->store
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);
}
got no idea at the moment where the bug is.
-
- 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
Can you post the error message ?
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: How to get $id immediately after jtable->store
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.peachio wrote:cannot workout why this is not working, can anyone analyse when i give more details...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); }
got no idea at the moment where the bug is.
Regards
-
- Joomla! Apprentice
- Posts: 16
- Joined: Mon Nov 15, 2010 11:08 am
Re: How to get $id immediately after jtable->store
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
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
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: How to get $id immediately after jtable->store
What happens if you do: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
echo get_class($model);
Is it a JTable object?
Regards
-
- Joomla! Apprentice
- Posts: 16
- Joined: Mon Nov 15, 2010 11:08 am
Re: How to get $id immediately after jtable->store
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
im already back home and cannot access my computer at work means i will check tomorrow moring. thank you for the tip..greetings peachio
-
- Joomla! Apprentice
- Posts: 16
- Joined: Mon Nov 15, 2010 11:08 am
Re: How to get $id immediately after jtable->store
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
- Posts: 16
- Joined: Mon Nov 15, 2010 11:08 am
Re: How to get $id immediately after jtable->store
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
in the model when store is called the id must be set again.
$this->setId($row->id); //resp. $model->setId
- sakiss
- Joomla! Explorer
- Posts: 348
- Joined: Wed Aug 20, 2008 4:09 pm
Re: How to get $id immediately after jtable->store
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:
You should use the primary key's name in order to work.
For example:
Code: Select all
$row->product_id