2
我有一個模型,名爲Admin with custom functions。Zend Framework 2實例化模型中的表格
<?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__);
}
}
}
和文檔說描述表,我們應該使用:
// 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
我怎麼可以設置管理員模式在我的表型,無需控制器?
謝謝,這是工作!但是幾個模型呢。我應該爲它們每個創建注射嗎? – ShiftedReality
當然,或者你可以通過服務定位器本身,這是你的選擇:) – Andrew