2012-02-14 4 views
-2

내 젠드 쿼리에 잘못된 점이 있습니까? Zend_Db_Table_Abstract 클래스를 확장하고 있습니까?성공적으로 행 추가시 아무 것도 반환하지 않는 Zend 쿼리

업데이트가 성공하면 업데이트는 0을 반환하며 이는 1이어야합니다. 새 사용자를 추가하면 삽입 성공시 아무 것도 반환하지 않습니다.

도와주세요.

/* 
* 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를 삽입하지 마십시오. 자동 증분 정수 기본 키입니다 (그렇지 않은 경우). 반환 된 값은 새로 삽입 된 행의 기본 키 값입니다.

그렇다면 코드는 괜찮은 것 같습니다. 당신의 USER_ID가 자동 증가이다

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

다음은 내 의견이 방법을 조금 개선하는 방법에 대한입니다. 그리고 업데이트 값이 기존 값과 동일하면 zend에서 업데이트하는 동안 항상 0이됩니다. DB에서 실제로 무언가를 업데이트 할 때만 데이터를 업데이트하면서 1이됩니다.

0

경우 그것을 제공하지 않습니다

관련 문제