Welcome Guest, Not a member yet? Register   Sign In
After save datamapper object, how do I get id
#1

[eluser]RJ[/eluser]
Once we run the $object->save() method how do we retrieve the ID this object was saved with?

thanks
#2

[eluser]IgnitedCoder[/eluser]
This works for me.

Code:
$object = new $obj();
$object->save_as_new();
//or
//using save implies an update so you generally want to already have an id assigned.
$object->id = 10;
$object->save(); //adds new if id is not supplied, updates existing if id is assigned.

echo $object->id;
#3

[eluser]RJ[/eluser]
$object->save_as_new();
Echo $object->id; (returns NULL)

$object->save();
Echo $object->id; (returns 0)

We could explicitly set the ID but would need to know the next #.

Any thoughts?
#4

[eluser]IgnitedCoder[/eluser]
Make sure your table has an id field of type int and that its set to auto increment/primary key. Remember the table name is plural and the Model is singular.

Code:
CREATE TABLE `tests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `thetext` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

Model

Code:
class Test extends DataMapper {

}

Controller

Code:
$test = new Test();
$test->thetext = 'test it';
$test->save();
echo $test->id;
#5

[eluser]RJ[/eluser]
We will check that out. Thanks Brendan




Theme © iAndrew 2016 - Forum software by © MyBB