[英]How to update an object in Zend Framework 2
i'm trying to update an object by using this code : The column co_nbre
will be updated to 0 !!!! 我正在尝试通过使用以下代码来更新对象:
co_nbre
列将更新为0! I think you will help me to fix this issue and thnx a lot. 我想您会帮助我解决这个问题,并且非常有用。
public function update($model) {
$data = get_object_vars($model);
$id = (int) $model->id;
$this->tableGateway->update($data, array('id' => $id));
}
and this is how did i use it in my controller: 这就是我如何在控制器中使用它:
if ($form->isValid()) {
$data = $form->getData();
$addi_info = new Addiinfo();
$addi_info->exchangeArray($data);
$addi_info->co_nbre = $request->getPost("co_nbre");
$addi_info->user_pin = $this->layout()->pin;
$addi_info->co_latitude = $request->getPost("latitude");
$addi_info->co_longitude = $request->getPost("longitude");
$addi_info->co_adresse = $request->getPost("adresse");
print_r($addi_info);die;
$checkuser=$this->getAddiinfoTable()->getAddiInfoByUserPin($user_pin);
if($checkuser[user_pin]==$user_pin){
$this->getAddiinfoTable()->update($addi_info);
I think you should create a function that returns associative array from model itself. 我认为您应该创建一个从模型本身返回关联数组的函数。
May be some of property in "Addiinfo" class be protected/private, so you need to get all property-value of model from inside it. 可能“ Addiinfo”类中的某些属性受到保护/私有,因此您需要从其中获取模型的所有属性值。
This one should be in your "Addiinfo" class, 这个应该在您的“ Addiinfo”类中,
public function getArrayData()
{
return get_object_vars($this);
}
Then call it in update function 然后在更新函数中调用它
public function update($model) {
$data = $model->getArrayData();
$id = (int) $model->id;
$this->tableGateway->update($data, array('id' => $id));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.