php - zend update is deleting old values

820

I am doing update in zend which in some cases doesn't update all the fields, the fields that are not updated become null as if we are doing an add.

This is the code from the Controller

$result = $theuserModel->updateUserTest(
                       $id,
                       $this->getRequest()->getPost('user_name'),
                       /*some code*/
                       $this->getRequest()->getPost('user_postee')
               );
if ($result) {
   $this->view->notif = "Successfull Update";
   return $this->_forward('index');
}

The corresponding model

public function updateUserRest($id, $nom,$poste)
{
    $data = array(
        'user_name' => $nom,
        'user_postee' => $poste
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
    return  $result;
}

I do an update for user_name only I found that the old value of user_postee got deleted and replaced by the default value (initial value which we get at the time of creation) for example null. Thanks in advance!

I have done this changes (bad solution) If anyone has another one optimised

->Controller

                    if($this->getRequest()->getPost('user_name')){
                    $resultname=$userModel->updateUserName($id,$this- 
                    >getRequest()->getPost('user_name'));
                    }

                    if($this->getRequest()->getPost('user_postee')){
                    $resultpostee=$userModel->updateUserPoste($id,$this- 
                    >getRequest()->getPost('user_postee'));
                    }
                    if ($resultname|| $resultpostee){
                    $this->view->notif = "Mise à jour effectuée";
                    return $this->_forward('index');
                          }

-> Model

        public function updateUserName($id, $name)
{
    $data = array(
        'user_name' => $name
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
   return  $result;
}           
        public function updateUserPostee($id, $postee)
{
    $data = array(
        'user_postee' => $poste
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
   return  $result;
}      
916

Answer

Solution:

that is complete correct response of update in Zend Db Table. I believe your assumption is if the value of 'user_postee' is null then it should not be updated into the database, am I correct. The answer is they will update the new value of "NULL" into the database. To avoid it , what you should do is using fetchrow() to get the value of the line by id foreach user_name and user_postee check if the value of them matching the array value your fetched , if nothing changed or Null, then use the old value from array , if new value exist use new value insert into the array , finally use update to update the new array into database

855

Answer

Solution:

Assume your Table Column is also "user_name" and "user_postee"

public function updateUserRest($id, $nom,$poste)
{
    $row = $this->fetchRow('user_id = '. (int)$id);
    if(!empty($nom) && $row['user_name'] != trim($nom)){
      $row['user_name'] = $nom;
    }   
    if(!empty($poste) && $row['user_poste'] != trim($poste)){
      $row['user_poste'] = $poste;
    }
$result=$this->update($row, 'user_id = '. (int)$id);
return  $result;
}

People are also looking for solutions to the problem: php - convert coordinates to json for upload to database

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.