[eluser]adwin[/eluser]
Hi .. I found a bug that not always happened in my model
i use ci 1.6.2
here is my code
Code:
function save()
{
$this->db->set('measureid',$this->measureid);
$this->db->set('goodsid',$this->goodsid);
$this->db->set('poid',$this->poid);
$this->db->set('qty',$this->qty);
$this->db->set('qty0',$this->qty0);
$this->db->set('unitprice',$this->unitprice);
$this->db->set('discount',$this->discount);
$this->db->set('total', $this->calculatetotal());
$this->db->trans_begin();
$status = '';
if($this->id != 0)
{
$this->db->where('id',$this->id);
$status = $this->db->update($this->_table);
}else
{
$status = $this->db->insert($this->_table);
$this->id = $this->db->insert_id() ;
}
if(!$this->checkstatus($status,$this->_table)){
echo $this->goodsid.' <br/>';
echo $this->db->last_query();
return false;
}
if(!$this->updateHeader($this->poid))
return false;
if ($this->db->trans_status()===FALSE){
return $this->rolllback();
} else {
$this->db->trans_commit();
return true;
}
}
as you can see, there is
Code:
$this->db->set('measureid',$this->measureid);
$this->db->set('goodsid',$this->goodsid);
on the top of the db->set();
measureid and goodsid is linked with other table (foreign keys)
when I did insert it produce the error because measureid or goodsid not included into the insert sql.
here is the error:
Code:
Cannot add or update a child row: a foreign key constraint fails (`ci/podt`, CONSTRAINT `podt_ibfk_2` FOREIGN KEY (`goodsid`) REFERENCES `mtgoods` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL : INSERT INTO `podt` (`measureid`, `qty`, `unitprice`, `discount`, `total`) VALUES ('1', 20, '30000', '300', 599700)
as you can see on my code, i have set the goodsid and measureid right ?