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?
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');