0
Zend Framework 2의 설명서에서 모델을 생성하여 테이블 작업 관리에 대해 살펴 보았습니다. exchangeArray() 메서드가있는 클래스가 필요합니까? 복사 데이터 일뿐입니다.// 하나의 모델을 만들어 몇 개의 테이블을 관리 할 수 있습니까?Zend framework 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();
좋아, 감사를하는 데 도움이,하지만 난 초기화 및 invokables에 따라 내 ExampleModel을 내가 만들 수있는 방법을 알고 사용하지 마십시오. '$ this-> getServiceLocator-> get (ExampleModel)'을 사용해야합니까? initializer 및 invokables 항목은 getServiceConfig()의 Module.php에 있어야한다고 가정합니다. –
물론, $ this-> getServiceLocator-> get (ExampleModel)을 사용하면 모델을 얻을 수 있습니다. Invokable은 모델이 인스턴스화되는 방식 일뿐입니다. 대체로 invokables는 서비스 이름과 인스턴스화 된 클래스 간의 직접 매핑입니다. 이니셜 라이저를 사용하면 인스턴스화 된 모델에 일부 소유권을 설정할 수 있습니다. 여기 좋은 기사가 도움이 될 것입니다. http://blog.alejandrocelaya.com/2014/10/09/advanced-usage-of-service-manager-in-zend-framework-2/ –
게시물에 코드가 있습니까? –