Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@kierzniak provided a good way to inject DbAdapter, My solution is almost the same, not only inject DbAdapter but the full ServiceLocator, By injecting ServiceLocator, we could also get system configs, EventManagers, I think it is more flexible. Here is the solution:</p> <p>Step 1: Make Album\Model implements ServiceLocatorAwareInterface, so that the service locator will be able to injected into Album\Model.</p> <pre><code>use Zend\ServiceManager\ServiceLocatorAwareInterface, Zend\ServiceManager\ServiceLocatorInterface; class Album AbstractModel implements ServiceLocatorAwareInterface, InputFilterAwareInterface { protected $serviceLocator; public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this-&gt;serviceLocator = $serviceLocator; return $this; } public function getServiceLocator() { return $this-&gt;serviceLocator; } } </code></pre> <p>Step2, Register Album\Model as a service in config or Module.php</p> <pre><code>class Module { public function getServiceConfig() { return array( 'invokables' =&gt; array( 'AlbumModel' =&gt; 'Album\Model\Album', ), ); } } </code></pre> <p>Step3, call AlbumModel by service but not a class:</p> <pre><code>public function addAction() { if ($request-&gt;isPost()) { $album = $this-&gt;getServiceLocator()-&gt;get('AlbumModel'); } } </code></pre> <p>Step4, now you could use DbAdapter in AlbumModel:</p> <pre><code>'adapter' =&gt; $this-&gt;getServiceLocator()-&gt;get('Zend\Db\Adapter\Adapter'); </code></pre> <p>By above steps, you could share any of services in anywhere, hope this could help you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload