我希望更新数据,如果存在于表Mysql中我添加数据,所以我执行此脚本,但只是添加不执行更新,如果已存在,如果id
public function addUpdateCust(array $data)
{
if(!empty($data))
{
if($this->update($data, array('id = ?' => $data['id'])) > 0 )
{
return true;
}
$row = $this->createRow();
//$row->id = $data['id'];
$row->code = $data['code'];
$row->fabname = $data['fabname'];
$row->namecustomer = $data['namecustomer'];
$row->adresse = $data['adresse'];
$row->city = $data['city'];
$row->zipcode = $data['zipcode'];
$row->country = $data['country'];
$row->phone = $data['phone'];
$row->representative = $data['representative'];
$row->idUser = NULL;
//var_dump($row);
try {
return $row->save();
} catch (Exception $e) {
return $e;
}
}
return false;
}
提前致谢
答案 0 :(得分:0)
你可能应该使用try / catch。更新不会像你想象的那样返回真或假。
public function addUpdateCust(array $data)
{
if(!empty($data))
{
try{
$this->update($data, array('id = ?' => $data['id']);
} catch (Zend_Exception $e) {
$row = $this->createRow();
//$row->id = $data['id'];
$row->code = $data['code'];
$row->fabname = $data['fabname'];
$row->namecustomer = $data['namecustomer'];
$row->adresse = $data['adresse'];
$row->city = $data['city'];
$row->zipcode = $data['zipcode'];
$row->country = $data['country'];
$row->phone = $data['phone'];
$row->representative = $data['representative'];
$row->idUser = NULL;
//var_dump($row);
try {
return $row->save();
} catch (Exception $e) {
return $e;
}
}
return true;
}
}
答案 1 :(得分:0)
public function addUpdateCust(array $data)
{
if(!empty($data))
{
$row = $this->createRow();
//$row->id = $data['id'];
$row->code = $data['code'];
$row->fabname = $data['fabname'];
$row->namecustomer = $data['namecustomer'];
$row->adresse = $data['adresse'];
$row->city = $data['city'];
$row->zipcode = $data['zipcode'];
$row->country = $data['country'];
$row->phone = $data['phone'];
$row->representative = $data['representative'];
$row->idUser = NULL;
//var_dump($row);
try {
return $row->save();
} catch (Zend_Exception $e) {
$this->update($data, array('id = ?' => $data['id']);
}
return true;
}
}