0

CakePHP3 is not returning me the id after insert the entity. itemData contain an array with the field values and the row was inserted in the DB. Any ideas?

In Cake Documentation saids that this is the way. Saving Data

$item = $this->Items->newEntity($itemData);
if ($this->Items->save($item)) {
    print_r($item->id);
}

Thanks!

mauriblint
  • 1,802
  • 2
  • 29
  • 46
  • 1
    Check the table involved. Does it have an auto_increment id? – gview Dec 11 '15 at 00:18
  • Thanks, i solve this adding: $this->primaryKey('id'); but i don't now why is not detecting this field automatic. I think there is an issue with my mysql table structure. – mauriblint Dec 11 '15 at 00:24
  • If the primary key column is named 'id' it should determine that. First question would be, what does your table class for Items look like? And by convention the name should be ItemsTable. From a mysql command line, when you do a "SHOW CREATE TABLE Items" what does that look like? You could update your question with that info, and it might help determine what is not working. – gview Dec 11 '15 at 00:36
  • @gview Actually it's determined by primary key constraints, the names don't matter. – ndm Dec 11 '15 at 01:19
  • 1
    do this.`if ($item=$this->Items->save($item)) {print_r($item->id);}`. its not showing id because entity doesn't have id before you are saving it.. – Sainesh Mamgain Dec 11 '15 at 04:45
  • @ndm: i'm not a cake user, although I've worked on a few cake based systems. According to the latest docs, which i perused, it claims to use the convention of id, and this can be overridden using the table->primaryKey() method in the table class. It would however make sense to do introspection I suppose, although doing this for every table is a lot of overhead. Doctrine2 does something similar but caches those data dictionary queries. Regardless I think we need table structure but the poster hasn't provided that. – gview Dec 11 '15 at 21:48
  • @gview True, but that convention has nothing to do with [**the detectability**](https://github.com/cakephp/cakephp/blob/3.1.5/src/Database/Schema/Table.php#L508) of the PK column (I'm not sure whether that convention is actually required anywhere at all anymore), it's not going to be detected without a PK constraint. Cake caches the schema too btw ;) But that's enough possible OT, the OP indeed needs to provide some details. – ndm Dec 11 '15 at 22:48
  • @ndm, not off topic at all, probably very relevant to the question, and good info for people reading this question in the future. Thx! – gview Dec 11 '15 at 23:09
  • question already answered here [GET ID AFTER SAVE](https://stackoverflow.com/questions/26027222/how-do-you-get-the-last-insert-id-in-cakephp-3-0) – Rod Nov 20 '17 at 15:32

2 Answers2

0

Try $this->Items->id. You are only saving the data from $item using the object $this->Items, thus the latter will contain the id.

JvO
  • 3,036
  • 2
  • 17
  • 32
0

I have this issue, too.

mysql :Ver 14.14 Distrib 5.5.47, for debian-linux-gnu (armv7l) using readline 6.2

This does not work.

$Location->save($ln);
echo $Location->id;  // error

This worked.

$L=$Location->save($ln);
echo $L->id;

I could not run like Cakephp 3.x manual...... :( It may depend on MySQL server version. because in my other PC, it worked.. c:\xampp\mysql\bin\mysql.exe Ver 14.14 Distrib 5.6.20, for Win32 (x86)

This work !

$Location->save($ln);
echo $Location->id;  // error

This did not work.

$L=$Location->save($ln);
echo $L->id;
Kazuhiko Nakayama
  • 801
  • 1
  • 8
  • 24