代码之家  ›  专栏  ›  技术社区  ›  Shamon S

在cakephp2.10中使用getLastInsertID时获取null

  •  -1
  • Shamon S  · 技术社区  · 6 年前

    在cakephp2.10中调用getLastInsertID()时获取空值

    Mycode是

    $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();
    

    如何解决这个问题?请帮忙

    1 回复  |  直到 6 年前
        1
  •  1
  •   Szymon    6 年前

    据我所知, Model::getLastInsertID() 在Cake 2.x中,只有通过模型方法而不是普通SQL查询进行插入时,才会返回最后插入的id。您应该尝试以下方法:

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