views:

497

answers:

2

I'm using Doctrine for database abstraction. Now I'd like to get the auto_increment primary key from the freshly-created (and save()'d) object - but $obj->toArray() shows me that the field is empty after calling save().

Is there a flag that I'm not aware of that does this? Or do I really have to query the object from the database?

A: 

Call refresh on the record instance before the toArray.

prodigitalson
Is this a 2.x or 1.2 feature? Because I'm on 1.1 and it doesn't make the primary key field appear. (but the refresh() method is there..)
Dave Vogt
Maybe it is only 1.2 - i thought it was 1.x though.
prodigitalson
+2  A: 

Ensure that you have the autoincrement flag set when setting up your object in the setTableDefinition() method (or related YAML config file). If this flag isn't set, then Doctrine won't know to update it. You should have something that looks like this:

 $this->hasColumn('id', 'integer', 4, array(
                  'type' => 'integer',
                  'length' => 4,
                  'fixed' => false,
                  'unsigned' => true,
                  'primary' => true,
                  'autoincrement' => true //this flag right here
             )
 );
Wickethewok
Aaah yea that was it! I accidentally wrote auto_increment.. I think doctrine should warn if a parameter isn't valid :( But Thanks anyhow!
Dave Vogt