Search code examples
phpcakephpcakephp-2.5

Getting null when using getLastInsertID in cakephp2.10


Getting empty value when calling getLastInsertID() in cakephp2.10

Mycode is

$this->query("INSERT INTO $savedtbl(ref_name,productdetails_id,users_id,type,image,saved_date,template_name) VALUES('$ref_name',{$paramsArray['Customimage']['productdetail_id']},'$uid','{$paramsArray['Customimage']['front_rear']}','$editedImg','$date','$template_name')");
$lastid = $this->getLastInsertID();

How to fix this? please help


Solution

  • As far as I know, Model::getLastInsertID() in Cake 2.x will return last inserted id only if that insert was made via Model methods, not plain SQL query. You should try this approach:

    $this->Model->create();
    $this->Model->set(...); //set your fields as needed
    $this->Model->save();
    $lastId = $this->Model->getLastInsertID();