update query in Zend Framework

Edward Maya picture Edward Maya · Apr 3, 2012 · Viewed 11.9k times · Source

what wrong with this Query ??? i need this Query.

UPDATE  user_preferences SET user_preferences_value = '2'
 WHERE user_preferences_name = 'is_user_package_active'
 AND   user_id = '$user_id'
 AND   phone_service_id='$phone_service_id';

is above query is equal to ZF query below

 function Deactivate_Service($user_id,$phone_service_id){
           $DB = Zend_Db_Table_Abstract::getDefaultAdapter();
           $data = array('user_preferences_value' => 2);
           $where = "user_preferences_name = 'is_user_package_active' AND user_id = " . (int)$user_id ." AND phone_service_id = ".(int)$phone_service_id;
           $DB->update('user_preferences',$data, $where);
      }

i am getting 0 with my ZF Query

EDITED:

public function deactivateserviceAction(){
       $this->_helper->viewRenderer->setNeverRender();
       $user = new Zend_Session_Namespace('user');
       $user_id =$user->user_id;
       $phone_service_id      = $this->_getParam('phone_service_id');
       //$Deactive = new Account();
        $DB = Zend_Db_Table_Abstract::getDefaultAdapter();
 $DB->query("UPDATE  user_preferences SET user_preferences_value = '2'
  WHERE user_preferences_name = 'is_user_package_active' AND   user_id = '$user_id' AND phone_service_id='$phone_service_id'");
      // $a = $Deactive->Deactivate_Service($user_id,$phone_service_id);
      // var_dump($a);

 }

Answer

Yuriy picture Yuriy · Apr 3, 2012

Your 'update' call seems to be fine, although in your case it is better to use the following syntax to build WHERE clause (but that's a stylistic thing):

$data = array('user_preference_value' => 2);

$where = array(
    'user_preferences_name = ?' => 'is_user_package_active',
    'user_id = ?' => $user_id,
    'phone_service_id = ?' => $phone_service_id
);

$DB->update('user_preferences', $data, $where);

So I suppose the problem here is with your default adapter. Are you sure you have set up the connection? Can you run SELECTs successfully with the same $DB object? Try running the plain SQL update with your object, i.e. $DB->query('Your raw UPDATE query here') to see if it works.

Also the standard way to obtain the default DB is from Zend_Db_Table, but that's also stylistic.