开发者

Make doctrine load auto_increment value after save

开发者 https://www.devze.com 2022-12-15 20:35 出处:网络
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() s开发者_如何学Gohows me that

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() s开发者_如何学Gohows 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?


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
             )
 );


Call refresh on the record instance before the toArray.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号