Zend Framework 2在Model中实例化表

Zend Framework 2在Model中实例化表

问题描述:

I have a Model, called Admin with custom functions.

<?php

namespace ZendCustom\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Exception\ErrorException;

abstract class Admin {

/**
 * @var TableGateway
 */
protected $_table;

/**
 * @var array 
 */
protected $data;

/**
 * @var bool
 */
protected $loaded = false;

/**
 * Constructor
 * 
 * @param array $data
 * @throws Exception
 */
public function __construct(array $data = array()) {
    if (!is_null($this->_table)) {
        if (!empty($data)) {
            $this->loaded = !empty($this->data);
        }
    } else {
        die('Please, specify table for ' . __CLASS__);
    }
}
}

And docs says to describe table, we should use:

// module/Album/src/Album/Controller/AlbumController.php:
    public function getAlbumTable()
    {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html

How could I set my Model Table inside Admin Model, without Controller?

我有一个模型,名为Admin with custom functions。 p>

 &lt;?php 
 
namespace ZendCustom \ Model; 
 
use Zend \ Db \ TableGateway \ TableGateway; 
use Zend \ Db \ Exception \ ErrorException; 
 
abstract class Admin {
 
 / ** / ** \  n * @var TableGateway 
 * / 
保护$ _table; 
 
 / ** 
 * @var数组
 * / 
保护$ data; 
 
 / ** 
 * @var bool \  n * / 
protected $ loaded = false; 
 
 / ** 
 *构造函数
 * 
 * @param数组$ data 
 * @throws Exception 
 * / 
 npublic function __construct(array $ data  = array()){
 if(!is_null($ this-&gt; _table)){
 if(!empty($ data)){
 $ this-&gt; loaded =!empty($ this-&gt  ;数据); 
} 
}其他{
 die('请,指定表'。__ CLASS __); 
} 
} 
} 
  code>  pre> 
 
  

文档说要描述表,我们应该使用: p>

  // module / Album / src / Album / Controller / AlbumController.php:
 public function getAlbumTable(  )
 {
 if if(!$ this-&gt; albumTable){
  $ sm = $ this-&gt; getServiceLocator(); 
 $ this-&gt; albumTable = $ sm-&gt; get('Album \ Model \ AlbumTable'); 
} 
返回$ this-&gt; albumTable;  
} 
  code>  pre> 
 
 

http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html p>

如何在没有Controller的情况下在Admin Model中设置我的模型表? p> div>

You can just inject it when it's instantiated via the Service Manager.

Module.php

/**
 * Get the service Config
 * 
 * @return array 
 */
public function getServiceConfig()
{
    return array(
        'factories' => array(
             'ZendCustom\Model\AdminSubclass' =>  function($sm) {
                // obviously you will need to extend your Admin class
                // as it's abstract and cant be instantiated directly
                $model= new \ZendCustom\Model\AdminSublcass();
                $model->setAlbumTable($sm->get('Album\Model\AlbumTable'));
                return $model;
            },
        )
    )
}

Admin.php

abstract class Admin {

    protected $_albumTable;

    /**
     * @param  \Album\Model\AlbumTable
     */
    public function setAlbumTable($ablum)
    {
        this->_albumTable = $ablum;
    }
}

Now if you want your Admin class (or a sublcass of it rather..) then you use the Service Manage to get the instance, and it will inject the Table Object you wanted..

Inside a controller you could do this:

$admin = $this->getServiceLocator()->get('ZendCustom\Model\AdminSubclass');