Wednesday, July 8, 2009

Tech Deck Companies Ranking

Tip: Solution to the Max (id) of MySQL in CakePHP

En determinadas ocasiones se requiere el ultimo id que se tiene en una base de datos en MySQL. La forma más práctica de hacer esto es usar la instrucción:

      1:     SELECT     MAX  (id)   as   MaxId   FROM   Table 




Ahora bien lograr esta instrucción en cakephp tiene su truco, algo que podemos hacer es usar la propiedad “query” de objeto model, pero el resultado es un poco difícil to access it throws an array of 3 dimensions. The easiest is to use the services of CakePHP and the model of access to managed data base, so there is a property find ('first') which will yield a single result, with the combination of parameters for order of the data we can find the last id:





  1:  $ maxid = $ this-> Model-> find ( 'first' ,   array ( 'order'  =>  'Model.id DESC' )); 



  2:  $ this-> set (  'maxid' , $ maxid [ 'Model' ] [ 'id' ]); 




This place it within a controller.



Ready!

0 comments:

Post a Comment