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(); 

답변

0

코드로 2 가지 작업을 수행해야합니다. 먼저 AdapterAwareInterface를 올바르게 구현하십시오. 둘째, 모델에 어댑터를 삽입하는 초기화 프로그램을 작성하십시오. 아래 코드를 고려하십시오.

... 

'initializers' => [ 
    function($instance, ServiceLocatorInterface $serviceLocator){ 
      if ($instance instanceof AdapterAwareInterface) { 
       $instance->setDbAdapter($serviceLocator->get('Zend\Db\Adapter\Adapter')); 
      } 
    } 
] 

... 

abstract class AbstractModel implements AdapterAwareInterface 
{ 
    protected $db; 

    public function setDbAdapter(Adapter $adapter) 
    { 
     $this->db = adapter; 
    } 
} 

... 

'invokables' => [ 
    'ExampleModel' => 'Application\Model\ExampleModel' 
] 

위에서 볼 수 있듯이, 결국 각 모델에 대한 팩토리가 필요하지 않습니다. invokables를 등록하거나 추상 팩토리를 만들어 모델을 인스턴스화 할 수 있습니다. 아래의 예를 참조하십시오

... 

'abstract_factories' => [ 
    'Application\Model\AbstractFactory' 
] 

... 

class AbstractFactory implements AbstractFactoryInterface 
{ 
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) 
    { 
     return class_exists('Application\Model\'.$requestedName); 
    } 

    public function createServiceWithName(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName) 
    { 
     $class = 'Application\Model\'.$requestedName(); 

     return new $class 
    } 
} 

희망이 도움

+0

좋아, 감사를하는 데 도움이,하지만 난 초기화 및 invokables에 따라 내 ExampleModel을 내가 만들 수있는 방법을 알고 사용하지 마십시오. '$ this-> getServiceLocator-> get (ExampleModel)'을 사용해야합니까? initializer 및 invokables 항목은 getServiceConfig()의 Module.php에 있어야한다고 가정합니다. –

+0

물론, $ this-> getServiceLocator-> get (ExampleModel)을 사용하면 모델을 얻을 수 있습니다. Invokable은 모델이 인스턴스화되는 방식 일뿐입니다. 대체로 invokables는 서비스 이름과 인스턴스화 된 클래스 간의 직접 매핑입니다. 이니셜 라이저를 사용하면 인스턴스화 된 모델에 일부 소유권을 설정할 수 있습니다. 여기 좋은 기사가 도움이 될 것입니다. http://blog.alejandrocelaya.com/2014/10/09/advanced-usage-of-service-manager-in-zend-framework-2/ –

+0

게시물에 코드가 있습니까? –