0

I looked through the manual of Zend Framework 2 about creating model to managing operations on table. Is the class with method exchangeArray() is necessary? It's only copy data :/ Can i create one model to manage a few tables?

I created two classes:

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;
        }
    }

and:

namespace Application\Model;

class ExampleModel extends AbstractAdapterAware
{

    public function fetchAllStudents()
    {

        $result = $this->db->query('select * from Student')->execute();

        return $result;
    }

}

I also add entries in 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'
    ],

I execute methods from model by:

$this->getServiceLocator()->get('ExampleModel')->fetchAllStudents();
Łukasz
  • 5
  • 3

1 Answers1

0

You should do 2 things with your code. First, implement AdapterAwareInterface properly. Second, create an initializer which injects the adapter into your model. Consider the code below:

...

'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'
]

As you can see from above, after all, you don't need a factory for each your model. You can either register invokables or create an Abstract Factory to instantiate your models. See an example below:

...

'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
    }
}

Hope this helps

Next Developer
  • 1,249
  • 1
  • 10
  • 20
  • Ok, thanks for help, but I don't know how can I create and use my ExampleModel based on initializers and invokables. Should I use `$this->getServiceLocator->get(ExampleModel)` ? I suppose that initializers and invokables entries should be in Module.php in getServiceConfig(). – Łukasz Oct 22 '14 at 19:48
  • Sure, you use $this->getServiceLocator->get(ExampleModel) to get your model. Invokable is just the way your model will be instantiated. All in all, invokables are direct mapping between service names and the classes from which they are instantiated. Initializers gives you an opportunity to set some proprieties to your model once it's been instantiated. Here's a good article may help you http://blog.alejandrocelaya.com/2014/10/09/advanced-usage-of-service-manager-in-zend-framework-2/ – Next Developer Oct 22 '14 at 20:21
  • It should, can you show your updated code in your post? – Next Developer Oct 22 '14 at 21:07
  • By the way, I wonder if it's possible to use singletons in ZF2? – Łukasz Oct 22 '14 at 21:26
  • Have you managed to fix that problem? Singletone is possible to use in any OOP system since its implementation is verysimple by its nature. However, it's not recomended to use too often, since it has some drawbacks. – Next Developer Oct 23 '14 at 06:14