2012-02-14 71 views
-2

我的zend查询有什么问题,这些扩展是Zend_Db_Table_Abstract类?Zend查询无法成功添加行也没有返回

更新成功更新时返回零(0)也应该是一个。添加新用户在成功插入时不返回任何内容。

请帮帮我。

/* 
* 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; 
} 

回答

3

请勿插入用户ID。这可能是一个自动增量整数主键(如果没有,它应该是)。返回的值恰好是新插入的行的主键值。

除此之外,代码似乎没问题。

0

,以下是我意见,关于如何提高这些方法一点点:

/* 
* 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; 
    } 
} 
0

如果user_id是自动增量则没有提供它。如果更新值与现有值相同,则在zend上更新时,您将始终得到0.只有当您更新数据库中的某些内容时,才会更新数据。