-2

Is something wrong with my zend queries these are extending Zend_Db_Table_Abstract class?

Update is returning zero (0) on successful update also it should be one. Adding new user is return nothing on successful insert.

Please help me.

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         'user_id' => '',
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    return $result;
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //echo $userId;
    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    return $result; 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vvr
  • 456
  • 3
  • 17
  • 30

3 Answers3

3

Do not insert the user ID. It's probably an auto increment integer primary key (if not, it should be). The returned value is precisely the newly inserted row's primary key value.

That apart, the code seems OK.

mingos
  • 23,778
  • 12
  • 70
  • 107
0

If your user_id is auto increment then don't provide it. And while updating on zend if your updating value is same as the existing one then you will always get 0. You will get 1 while updating the data only when you are really updating something in your DB.

0

The following is my opinion on how to improve these methods a little bit:

/*
* Adding new user into database
* @params unknown_type $title, $firstName, $lastName
* @return boolean $sql
*/
public function addNewUser( $title, $firstName, $lastName ) {
    $data = array (
         //'user_id' => '',  not needed fir insert if is primary key and auto incrementing
         'title' => $title,
         'first_name' => $firstName,
         'last_name' => $lastName,
    );
    $result = $this->insert($data);
    //Don't take chances, return know values. All you need to know at the controller is yes or no
    if (! $result) {
        return FALSE;  //or you can throw an exception
    } else {
        return TRUE;
    }
}

/*
* updating user details using given user id
* @params unknown_type values $userId, $title, $firstName, $lastName
* @return nuumber of rows update, hopefully 1
*/
public function updateUserDetails( $userId, $title, $firstName, $lastName )
{   //you forgot the title array key
    //if you are concerned there might be more then one row with this id test for it before the update.
    $rows = $this->fetchAll($userId);
    if ($rows->count() > 1) {
        throw new Exception()
     }        


    $data = array ( 'first_name' => $firstName, 'last_name' => $lastName, 'title' =>$title );
    $where = $this  ->getAdapter()
                    ->quoteInto('user_id = ?', $userId);
    $result = $this ->update($data, $where );
    //again ensure your method returns values that you want
    if (! $result) {
        return FALSE; //or exception or 0 or 'yankee doodle'
    } else {
    return TRUE; 
    }
}
RockyFord
  • 8,529
  • 1
  • 15
  • 21