1

私はZend Frameworkの初心者です。

私はこのコードの構造を持っています。

DB テーブル

 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('*');
 }

しかし、すべての従業員のすべてのデータを取得することはできません。

4

4 に答える 4

2
public function selectAllEmployees(){
  $tblEmployee = new Application_Model_DbTable_Employee();
  return $tblEmployee->fetchAll($tblEmployee->select());
}
于 2013-04-04T06:22:12.960 に答える
1

モデルでこのコードを試してください:

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

詳細については、http://framework.zend.com/manual/1.12/en/zend.db.table.rowset.html#zend.db.table.rowset.to-array を参照してください。

于 2013-04-04T06:27:01.420 に答える
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);
}

具体的なモデルは次のようになります。

<?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_Mapper_Employee();
    $employees = $model->findAll();
}

データベースにクエリを実行する最も直接的な方法は、コントローラーから直接、最も推奨されない方法です。

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

これがあなたの助けになり、あまり混乱しないことを願っています.

于 2013-04-04T10:05:30.593 に答える
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();
于 2013-04-04T07:28:31.843 に答える