0

I just inherited some cakePHP code and I am not very familiar with it (or any other php/serverside language). I need to set the id of the item I am adding to the database to be the value of the last item plus one, originally I did a call like this:

$id = $this->Project->find('count') + 1;

but this seems to add about 8 seconds to my page loading (which seems weird because the database only has about 400 items) but that is another problem. For now I need a faster way to find the id of the last item in the database, is there a way using find to quickly retrieve the last item in a given table?

Mike2012
  • 7,629
  • 15
  • 84
  • 135

3 Answers3

5

That's a very bad approach on setting the id.

You do know that, for example, MySQL supports auto-increment for INT-fields and therefore will set the id automatically for you?

The suggested functions getLastInsertId and getInsertId will only work after an insert and not always.

I also can't understand that your call adds 8 seconds to your siteload. If I do such a call on my table (which also has around 400 records) the call itself only needs a few milliseconds. There is no delay the user would notice.

I think there might be a problem with your database-setup as this seems very unlikely.

Also please have a look if your database supports auto-increment (I can't imagine that's not possible) as this would be the easiest way of adding your wanted functionality.

Tim
  • 5,893
  • 3
  • 35
  • 64
  • Thank you very much! I got the autoincrement to work and then I was able to retrieve the id with a call to getLastInsertID after adding the data to the model! – Mike2012 Jun 08 '11 at 17:08
1

I would try

$id = $this->Project->getLastInsertID();
$id++;

The method can be found in cake/libs/model/model.php in line 2768

As well as on this SO page

Cheers!

Community
  • 1
  • 1
JimP
  • 1,070
  • 15
  • 26
  • I tried that, but $this->Project->getLastInsertID(); always returns 0 – Mike2012 Jun 07 '11 at 22:40
  • what about $this->Project->getInsertID();? It's very similar. Also, what version of cakephp are you running? 1.2? 1.3? – JimP Jun 07 '11 at 22:43
  • $this->Project->getInsertID(); also seems to return 0, I'm using 1.2.8. – Mike2012 Jun 07 '11 at 22:51
  • maybe you could go for something along these lines instead? it's not as elegant but it is a solution. `$lastItem = $this->Project->find('first', array('order' => array('Project.created DESC')));` – JimP Jun 08 '11 at 00:12
  • Sadly that seems as slow as count. Maybe the real question I need to ask is why getLastInsterID always returns 0. – Mike2012 Jun 08 '11 at 01:01
  • It returns 0 because there was no insert. – dhofstet Jun 08 '11 at 04:54
  • @dhofstet - You make a good point, as did @Tim in the answer above. – JimP Jun 08 '11 at 14:51
0

If you are looking for the cakePHP3 solution to this you simply use last().

ie:

use Cake\ORM\TableRegistry;
....
$myrecordstable=Tableregistry::get('Myrecords');
$myrecords=$myrecordstable->find()->last();
$lastId = $myrecords->id;
....
Frank
  • 21
  • 2