2012-11-19 3 views
1

필자는 모델 클래스에 다음과 같은 두 가지 함수를 작성했습니다.젠드 프레임 워크에서 로우 테이블 클래스를 작성하는 방법은 무엇입니까?

public function fetchByUsername($username) 
     { 
      $select=$this->select(); 
      $select->where('username = ?', $username) ; 
      $user = $this->fetchRow($select); 
      return $user; 
     } 

    public function fetchByPhone($phone) 
     { 
      $select = $this->select(); 
      $select->where('phone = ?', $phone) ; 
      $user = $this->fetchRow($select); 
      return $user; 
     } 

하지만 내 행 테이블 클래스에 fetch 함수를 작성하고 위의 두 함수를 작성하지 않고 모델 클래스에서 해당 값에 액세스하려고합니다. 도와주세요.

감사

User.php

class Model_User extends Model_DbTable_Row 
{ 
    //here i need to write fetch function like below 
    // public function fetchPhone() 
    // { 
    // return $this->phone; 
    // } 

    // public function fetchUsername() 
    // { 
    // return $this->username; 
    // } 
} 

Users.php

class Model_Users extends Model_DbTable_Abstract 
{ 
    protected $_name  = 'users'; 
    protected $_primary = 'userId'; 
    protected $_rowClass = 'Model_User'; 

    protected $_saveInsertDate = true; 
    protected $_saveUpdateDate = true; 
    } 

UsersController.php는

class Admin_UsersController extends BusinessForum_Admin_Controller 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
     $this->view->pageTitle = 'Users - ' . $this->view->pageTitle; 
     parent::init(); 
    } 

    public function indexAction() 
    { 
     // get name of the director 
     $userId = $this->_getParam('directorId'); 
     if ($userId) { 
     $modelUsers = new Model_Users(); 
     $user = $modelUsers->fetch($userId); 
     $fullName = $user->firstName." ".$user->lastName; 
     $directorName = "Direcotor : ".$fullName; 
     $this->view->directorName = $directorName; 
     } 
    } 

답변

1
public function fetchDetails($attr,$value) 
     { 
      $select=$this->select(); 
      if($attr == 'username'){ 
      $select->where('username = ?', $value) ; 
      else if($attr == 'phone '){ 
      $select->where('phone = ?', $value) ; 
      } 
      $user = $this->fetchRow($select); 
      return $user; 
     } 

그리고 있습니다

//user 
    $details = $your_model->fetchDetails('username',$usernamedata); 

    //phone 
    $details = $your_model->fetchDetails('phone',$phonedata); 

또는 밖으로 모델과 모델 기능

$obj = new Yournamespace_Model_Yourfile(); 
$where = $obj->getAdapter()->quoteInto('username = ?',$username); 
$result = $obj->fetchRow($where); 

출력 및 사용과 // 사용자 이름과 폰으로 과 같이 호출 동일

$where = array(); 
    $obj = new Yournamespace_Model_Yourfile(); 
    $where[] = $obj->getAdapter()->quoteInto('username = ?',$username); 
    $where[] = $obj->getAdapter()->quoteInto('phone = ?',$phone); 
    $result = $obj->fetchRow($where); 
    //if you have more than one row then use 
    $result = $obj->fetchAll($where); 

갱신 OP의 요청

에 따라 같은 가져 오는 한 후이 $result = $obj->fetchRow($where);

이러한

$result->phone; 

또는

$result->username; 
+0

공공 기능 가져 오기 ($ 사용자 ID) \t 같은 개별 요소를 얻을 수 있습니다 { \t \t $ select = $ this-> select(); \t \t $ select-> where ('userId =?', (int) $ userId); \t \t $ select-> where ('active =?', 'Y'); \t \t return $ this-> fetchRow ($ select); \t} 위의 가져 오기 기능을 쓰고 사용자 ID가 필요하면 전화 번호 또는 사용자 이름에 액세스하려고 시도합니다. – user1559230

+0

@ user1559230 불량 업데이트 내 대답 – coolguy

+0

업데이트 ... 대답 – coolguy

0

당신은 단지에 public function fetchRow($selectObjet) 함수를 작성 할 수 있습니다 테이블 클래스 (Application_Model_DbTable_TableName). 그러면 기본 fetchRow 메서드가 재정의됩니다. 해당 함수에서 parent::fetchRow을 호출하여 기본 결과를 가져 와서 결과를 수정할 수도 있습니다.

관련 문제