将last_inserted id从一个函数传递到codeigniter中同一类中的另一个函数

时间:2019-02-04 05:09:54

标签: php function codeigniter class mysqli

我想将变量$ lastid传递给同一类中的另一个函数。我该怎么办?? ..我尝试了一些方法,但是没有用?

accessibilityIdentifier

这是下一个功能

class ProductmModel extends CI_Model
{

  var $PRODUCTNAME='';
  var $DESCRIPTION='';
  var $CATEGORY='';
  var $SUBCAT='';
  var $IMAGE='';


public function addproductm()
{
$this->load->database();
$data = array(
"p_name" => $this->PRODUCTNAME,
"p_des" => $this->DESCRIPTION,
"cat" => $this->CATEGORY,
"subcat" => $this->SUBCAT

);
$this->db->insert('product_multi', $data);
$lastid = $this->db->insert_id();

}

3 个答案:

答案 0 :(得分:0)

首先,请确保您的表中有一个自动递增的ID

第二,插入后从模型返回$this->db->insert_id()

public function addproductm()
{
    $this->load->database();
    $data = array(
       "p_name" => $this->PRODUCTNAME,
       "p_des" => $this->DESCRIPTION,
       "cat" => $this->CATEGORY,
       "subcat" => $this->SUBCAT
    );
    $this->db->insert('product_multi', $data);
    return $this->db->insert_id();
}

在这里您最后一次在模型中插入了id,例如$last_id = $this->db->insert('image', $data)

$last_id传递给功能

public function addimage()
{
   $last= $this->addproductm();
   $this->load->database();

   $data = array(
      "pid" => $lastid,
      "images" => $this->IMAGE
   );
   $last_id = $this->db->insert('image', $data);
   $this->function_name($last_id);
}

答案 1 :(得分:0)

您可以在第一个函数中调用addimage函数。我希望这会有所帮助。

public function addproductm()
{
$this->load->database();
$data = array(
"p_name" => $this->PRODUCTNAME,
"p_des" => $this->DESCRIPTION,
"cat" => $this->CATEGORY,
"subcat" => $this->SUBCAT
);
$this->db->insert('product_multi', $data);
$lastid = $this->db->insert_id();
$id=$this->addimage($lastid);
}

public function addimage($id)
{
$last= $this->addproductm();
$this->load->database();

$data = array(
"pid" => $id,
"images" => $this->IMAGE

);
$this->db->insert('image', $data);
}
}

答案 2 :(得分:0)

这是一种方法。假设您有

之类的函数
function getCirclePoints($center,$radius){

}

从同一类调用函数时

$this->getCirclePoints($latlng,radius));