2013-04-04 62 views
1

我是Zend框架中的新成員。在Zend框架中選擇命令

我有這種結構的代碼。

DBTABLE

class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract 
{ 

protected $_name = 'tab_employee'; 
} 

型號

public function selectAllEmployees(){ 
    $tblEmployee = new Application_Model_DbTable_Employee(); 
    $tblEmployee->select('*'); 
} 

但我能不能讓所有的員工的所有數據。

+0

任何人都可以幫我找到解決方案嗎? – student 2013-04-04 05:22:18

回答

2
public function selectAllEmployees(){ 
    $tblEmployee = new Application_Model_DbTable_Employee(); 
    return $tblEmployee->fetchAll($tblEmployee->select()); 
} 
0

模型函數

public function selectAllEmployees() 
    { 
     $selectSql = $this->select(); 
     $selectSql->from($this->_name, array('*')) 
        ->order(id DESC');  

     return $this->fetchAll($selectSql); 
    } 

$employeeModel = new Application_Model_Employee(); 
$employees = $employeeModel->selectAllEmployees(); 
+0

這是不完整的,你忘記訪問'dbTable'作爲網關。這段代碼可以在dbTable本身中工作 – RockyFord 2013-04-04 09:35:52

0

Maks3w是正確的,以及簡潔。

這裏有一些更詳細的信息。

有很多方法可以使用您的Application_Model_DbTable_Employee來訪問和查詢您的數據庫的tab_employee表。

最簡單的方法是直接從dbTable模型本身來查詢:

class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract 
{ 

    protected $_name = 'tab_employee'; 

    public function selectAllEmployees() 
    { 
     //$this refers to the current dbTable object 
     $select = $this->select(); 
     //when querying from the dbTable the from() parameter is assumed to be $this->_name, array('*') 
     //there several other sql commands available to the select(), where(), orWhere(), join() 
     $select->order('id DESC'); 

     $result = $this->fetchAll($select); 

     return $result; 
    } 
} 

控制器代碼:

public function indexAction(){ 
     model = new Application_Model_DbTable_Employee(); 
     $employees = $model->selectAllEmployees(); 
     $this->view->employees = $employees 
    } 

通常映射器模型被用於訪問數據庫和將數據提供給一個實體模型。這也很常見,相對來說很簡單。要記住的關鍵項目時設計的映射是包括代碼來訪問DBTABLE模型數據庫適配器,通常被稱爲gateway,這裏是一些示例代碼:

<?php 
//This is one way to build a mapper 
abstract class My_Model_Mapper_Abstract 
{ 
    /** 
    * Instance of Zend_Db_Table_Abstract 
    */ 
    protected $tableGateway = null; 

    /** 
    * Will accept a DbTable model passed or will instantiate 
    * a Zend_Db_Table_Abstract object from table name. 
    */ 
    public function __construct(Zend_Db_Table_Abstract $tableGateway = null) 
    { 
     if (is_null($tableGateway)) { 
      $this->tableGateway = new Zend_Db_Table($this->_tableName); 
     } else { 
      $this->tableGateway = $tableGateway; 
     } 
    } 

    /** 
    * Get default database table adapter 
    */ 
    protected function getGateway() 
    { 
     return $this->tableGateway; 
    } 

    /** 
    * findAll() is a proxy for the fetchAll() method and returns 
    * an array of entity objects. 
    * 
    * @param $where, primary key id 
    * @param string $order in the format of 'column ASC' 
    * @return array of entity objects 
    */ 
    public function findAll($where = null, $order = null) 
    { 
     $select = $this->getGateway()->select(); 

     if (!is_null($where)) { 
      $select->where('id = ?', $where); 
     } 
     if (!is_null($order)) { 
      $select->order($order); 
     } 
     $rowset = $this->getGateway()->fetchAll($select); 

     $entities = array(); 
     foreach ($rowset as $row) { 
      $entity = $this->createEntity($row); 
      $this->setMap($row->id, $entity); 
      $entities[] = $entity; 
     } 

     return $entities; 
    } 

    /** 
    * Abstract method to be implemented by concrete mappers. 
    */ 
    abstract protected function createEntity($row); 
} 

具體模式可能類似於:

直接

public function indexAction(){ 
    model = new Application_Model_Mapper_Employee(); 
    $employees = $model->findAll(); 
} 

和最直接的方式來查詢你的數據庫是最推薦的方式,:

<?php 

class Application_Model_Mapper_Employee extends My_Model_Mapper_Abstract 
{ 
    protected $tableName = 'tab_employee'; 
    //I hard code the $tableGateway though this is probably not the best way. 
    //$tableGateway should probably be injected, but I'm lazy. 
    public function __construct(Zend_Db_Table_Abstract $tableGateway = null) 
    { 
     if (is_null($tableGateway)) { 
      $tableGateway = new Application_Model_DbTable_User(); 
     } else { 
      $tableGateway = $tableGateway; 
     } 
     parent::__construct($tableGateway); 
    } 
    //create the Entity model 
    protected function createEntity($row) 
    { 
     $data = array(
      'id'  => $row->id, 
      'name'  => $row->name, 
      'password' => $row->password, 
     ); 
     $employee = new Application_Model_Employee($data); 

     return $employee; 
    } 
} 

使用該控制器中的可能看起來像從控制器:

public function indexAction(){ 
    model = new Application_Model_DbTable_Employee(); 
    $employees = $model->fetchAll(); 
    $this->view->employees = $employees 
} 

我希望這爲您提供一些幫助,而不是許多混亂。