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?
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
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.
If you are looking for the cakePHP3 solution to this you simply use last().
use Cake\ORM\TableRegistry; .... $myrecordstable=Tableregistry::get('Myrecords'); $myrecords=$myrecordstable->find()->last(); $lastId = $myrecords->id; ....
C-is char*str中的字符串实际上等同于char str？
php preg_match regex-match chars从a到z，应该包含@char
由于相关产品状态未知，SQL Server 2008 R2 SMO引导程序安装失败