update query in Zend Framework

12,575

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.

Share:
12,575
Edward Maya
Author by

Edward Maya

new to ZF..

Updated on June 04, 2022

Comments

  • Edward Maya
    Edward Maya almost 2 years

    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);
    
     }
    
  • Edward Maya
    Edward Maya about 12 years
    my $DB object is working all over the project except in this case and i dont know why this is not working
  • Yuriy
    Yuriy about 12 years
    If you can't run SELECT's with it as well there is the only answer - your DB connection is not initialised properly in that branch of execution (basing on the fact it works elsewhere in your app). Impossible to tell where exactly without knowing your code, try to trace and debug to make sure you run the initialisation with proper credentials.
  • Edward Maya
    Edward Maya about 12 years
    i try this $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'");
  • Edward Maya
    Edward Maya about 12 years
    but its not working too,but the class in which this function is i have another functions and there $DB is working how to debug ???
  • Edward Maya
    Edward Maya about 12 years
    if $db is not initialized correct t this is not giving me the error.as a matter of fact no error t all and its not updating the record
  • Yuriy
    Yuriy about 12 years
    Sorry, but it is nearly impossible to tell what exactly is wrong without looking at the code. If you're sure $DB is initialised properly try calling it outside this function where before it's invoked, then again outside etc. to find the point at which it breaks.
  • Edward Maya
    Edward Maya about 12 years
    when i do so echo $DB->update('user_preferences', $data, $where); its printing 0
  • Yuriy
    Yuriy about 12 years
    That means your query succeeds but 0 rows match it. Which is legit when there are no rows in your table matching your WHERE condition. Make sure please there are rows matching that WHERE and try again.