モデルの作成からテーブルの操作の管理については、Zend Framework 2 のマニュアルを参照しました。メソッド exchangeArray() を持つクラスは必要ですか? データのコピーのみです:/ いくつかのテーブルを管理するために 1 つのモデルを作成できますか?
2 つのクラスを作成しました。
namespace Application\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;
abstract class AbstractAdapterAware implements AdapterAwareInterface
{
protected $db;
public function setDbAdapter(Adapter $adapter)
{
$this->db = $adapter;
}
}
と:
namespace Application\Model;
class ExampleModel extends AbstractAdapterAware
{
public function fetchAllStudents()
{
$result = $this->db->query('select * from Student')->execute();
return $result;
}
}
Module.php にもエントリを追加します。
'initializers' => [
'Application\Model\Initializer' => function($instance, \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator){
if ($instance instanceof AdapterAwareInterface)
{
$instance->setDbAdapter($serviceLocator->get('Zend\Db\Adapter\Adapter'));
}
}
],
'invokables' => [
'ExampleModel' => 'Application\Model\ExampleModel'
],
次の方法でモデルからメソッドを実行します。
$this->getServiceLocator()->get('ExampleModel')->fetchAllStudents();