2

I am fairly new to CakePHP and having a problem getting last inserted id. My schema is as follows:

CREATE TABLE IF NOT EXISTS `markets` (
  `id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `region_code` varchar(5) NOT NULL,
  `country_code` varchar(255) NOT NULL,
  `language` varchar(255) NOT NULL,
  `default_language` varchar(10) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

ALTER TABLE `markets`
   ADD PRIMARY KEY (`id`),
   ADD UNIQUE KEY `region_code` (`region_code`),
   ADD KEY `locale` (`country_code`),
   ADD KEY `language` (`language`);

ALTER TABLE `markets`
   MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;

And this is my logic:

$this->loadModel('Market');
if( isset($this->request->data['Market']) ) {
  $this->Market->set($this->request->data);
  if( $this->Market->validates() ) {
    $this->Market->save($this->request->data);
    echo $this->Market->id; //displays zero
    echo $this->Market->getLastInsertID(); //also displays zero
  }
}

It all looks good in MySQL though, as id gets auto incremented there as it's supposed to be.

P.S. I just inserted another record and here's what's returned:

array (size=1)
  'Market' => 
    array (size=8)
      'name' => string 'Japan' (length=5)
      'region_code' => string 'JP' (length=2)
      'country_code' => string 'JP' (length=2)
      'language' => string 'ja_JP' (length=5)
      'default_language' => string 'ja_JP' (length=5)
      'modified' => string '2015-04-23 09:11:34' (length=19)
      'created' => string '2015-04-23 09:11:34' (length=19)
      'id' => string '0' (length=1)

Here's the saved data in phpMyAdmin

Ok, so for now I just added a function called 'getMarketLastInsertId' to the Market model, which takes 'country_code' (unique key in the table) as a parameter and returns the last id... Thanks

petermk
  • 757
  • 1
  • 6
  • 18
  • 1
    you could try $this->ModelName->getInsertID(); – Enjoyted Apr 23 '15 at 12:51
  • $this->Market->getInsertId(); returns zero as well – petermk Apr 23 '15 at 13:00
  • Have you properly constructed the array to save to the database? You are not checking that the save completes sucessfully – Colonel Mustard Apr 23 '15 at 13:03
  • $result = $this->Market->save($this->request->data); $record_id=$result->id; – kamlesh.bar Apr 23 '15 at 13:04
  • are you sure you are saving anything to the database? Why not doing if($this->Market->save($this->request->data)) { // print stuff } – Enjoyted Apr 23 '15 at 13:05
  • Yes, I am sure it's saving correctly. As I said in my original post, "it all looks good in MySQL though, as id gets auto incremented there as it's supposed to be." – petermk Apr 23 '15 at 13:08
  • @petermk does data saved successfully to Database otherwise you won't get returned id? – kamlesh.bar Apr 23 '15 at 13:14
  • Yes, it's being saved correctly. Then, when I do an edit, getLastInsertId() returns the correct id saved in the DB. It's new records that always return zero. – petermk Apr 23 '15 at 13:15
  • before the `$this->Market->save($this->request->data);` add this call `$this->Market->create();` that should set the `$this->Market->id` var to the new id to be inserted – Yerko Palma Apr 23 '15 at 14:09
  • That did not work either, still getting zero. – petermk Apr 23 '15 at 14:47
  • Are you setting any value to id _before_ saving the data? by the way, if you use `$this->Market->set($this->request->data)`, you don't need to pass data again to `save()` – Choma Apr 23 '15 at 15:08
  • I have a form field echo $this->Form->input('id', array('type' => 'hidden', 'class' => 'mediuminput')); but I never set its value anywhere in my code. Your suggestion returned zero as well. – petermk Apr 23 '15 at 19:51

3 Answers3

2

In the create table statement you have shown

`id` int(11) NOT NULL, 

should actually be

`id` int(11) NOT NULL AUTO_INCREMENT, 

for id to be an auto increment field. So I doubt your claims about the field getting filled with auto increment values.

ADmad
  • 8,102
  • 16
  • 18
  • 1
    Hm, I've been using MySQL for over 10 years and know how to use phpMyAdmin as well. The values are being saved and auto incremented. Thank you. – petermk Apr 23 '15 at 13:23
  • 3
    @petermk Even experienced users can make a mistake. ADmad is right, this schema will not auto increment. See the proof here: http://i.imgur.com/cE8dzEP.png – Oldskool Apr 23 '15 at 13:30
  • The schema above was a phpmyadmin export and I forgot to include ALTER TABLE statements from the bottom of the exported file. Sorry about that. – petermk Apr 23 '15 at 14:49
  • 2
    Over 10 years of experience still doesn't prevent simple human errors ;) – ADmad Apr 23 '15 at 15:01
  • The database works as it should, so there was no error on my part. – petermk Apr 23 '15 at 15:08
1

In CakePHP 3.X.X you can do like this

$result = $this->Market->save($this->request->data);
$record_id=$result->id;

More general

$result=$this->ModelName->save($whatever);
$record_id=$result->id;

For More Info

http://book.cakephp.org/3.0/en/orm/saving-data.html#a-glance-over-saving-data http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-entities

kamlesh.bar
  • 1,774
  • 19
  • 38
0

You don't have you primary key set up correctly. You should have NOT NULL and AUTO_INCREMENT applied to your id column as well has having it defined as you primary key.

John williams
  • 654
  • 1
  • 8
  • 22
  • You missed this: ALTER TABLE `markets` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8; – petermk Apr 24 '15 at 19:34