php - 将last_inserted id从一个函数传递给codeigniter中同一类中的另一个函数
问题描述
我想将我的变量 $lastid 传递给同一类中的另一个函数。我该怎么做??..我尝试了一些东西,但它不起作用?
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();
}
这是下一个功能
public function addimage()
{
$last= $this->addproductm();
$this->load->database();
$data = array(
"pid" => $lastid,
"images" => $this->IMAGE
);
$this->db->insert('image', $data);
}
}
解决方案
首先,确保您的表中有一个自动递增的 id
二、$this->db->insert_id()
插入后返回from模型
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
to 函数
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);
}
推荐阅读
- python - Cython:扩展类型的链接列表
- javascript - 来自验证的 Javascript 点击事件
- jquery - 在按钮单击时循环遍历元素,然后全部隐藏,然后使用 jQuery 再次开始循环
- python - 在 Django 模型中删除新列或删除现有表时如何保留以前的数据?
- kotlin - 如何修复所需的 Long 发现 Long?
- unit-testing - Rust 单元测试 - 函数调用在最后挂起并且不返回
- apache-storm - Apache Storm 元组在 10 分钟后超时,但 topology.message.timeout.secs 配置为 5 分钟
- python - Tkinter 被线程冻结
- python - 防止aws lambda代码多次执行
- scala - 如何使用 spray-json 将 jsvalue 转换为 Map[string,Any]?