try to change this:
$order = array("GalProvider.id DESC");
to this:
$order = array('GalProvider.id' => 'desc');
or try this:
$gal_provider_id = $this->GalProvider->field("id",array('order' => array('GalProvider.id' => 'desc')));
Question
I am trying to extract the latest id
of the table called gal_providers
//get the id of the last gal_provider add.
$order = array("GalProvider.id DESC");
$gal_provider_id = $this->GalProvider->field("id",array("order"=>$order));
$this->data["User"]["username"] = "gal_provider_".$gal_provider_id;
But it shows error like :
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order = ('GalProvider.id DESC') LIMIT 1' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: SELECT
GalProvider
.id
FROMgal_providers
ASGalProvider
WHERE order = ('GalProvider.id DESC') LIMIT 1
Whats wrong with the code ?
No correct solution
OTHER TIPS
try to change this:
$order = array("GalProvider.id DESC");
to this:
$order = array('GalProvider.id' => 'desc');
or try this:
$gal_provider_id = $this->GalProvider->field("id",array('order' => array('GalProvider.id' => 'desc')));
As noted in the CakePHP documentation, the second argument of the Model::field() method is for a condition, and the third is for the order.
Also looking at the documentation, we can see that by default the condition and order arguments are null - which means we can do the same.
Try this:
$gal_provider_id = $this->GalProvider->field("id", null, "id DESC");
$this->data["User"]["username"] = "gal_provider_".$gal_provider_id;
In your example, you're using the Model::field() method as if it were the Model::find() method (they are two different methods and require different formats for their arguments)
Alternatively, we can use the find method itself:
$gal_provider = $this->GalProvider->find('first', array(
'fields' => array('GalProvider.id'),
'order' => 'GalProvider.id DESC',
'recursive' => -1 // more efficient (ensures no joins made for the query)
));
$this->data["User"]["username"] = "gal_provider_".$gal_provider['GalProvider']['id'];