Zend Framework 2 - 在模型中调用映射器函数的最佳方法

I preferred double layer models (mapper and model) over doctrine in my zend framework 2 project and trying to make them work little bit like doctrine so I can access relational data from the models (entities). Following example demonstrates what I am trying to achieve.

class User
{

    protected $userTable;
    public $id;
    public $name;

    public function __construct($userTable)
    {
        $this->userTable = $userTable
    }

    public function getUserArticles()
    {
        return $this->userTable->getUserArticles($this->id);
    }
}

Problem is I cannot inject my user table in my user model, because table gateway uses model class as array object prototype which gets later injected to create user table gateway (mapper).

I don't want to inject service manager in my models as it is considered as a bad practice. How can I inject my user table in my user model? is it possible? what is the best way to achieve what I am trying to do

You should not inject your mapper into your model, that's exactly the other way around. Important for you to understand is the way the relations work and models shouldn't have any knowledge how their data is mapped to a persistency framework.

You refer to Doctrine, so I'd suggest you also look at how Doctrine solves this problem. The way they do it is via a Proxy. A proxy is a generated class (you need to write your own generator or write all proxies yourself) which extends the model and have the mapper injected:

class Foo
{
    protected $id;
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

class ProxyFoo extends Foo
{
    protected $mapper;

    public function __construct(FooMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    public function getName()
    {
        if (null === $this->name) {
            $this->load();
        }

        return parent::getName();
    }

    protected function load()
    {
        $data = $this->mapper->findById($this->id);

        // Populate this model with $data
    }
}

My suggestion: look either at the default mapper pattern Zend Framework 2 applies and forget lazy loading, or just use Doctrine. This is too much work to get this done properly.

What you are trying to do is mix two design patterns: Active Record and Data Mapper.

If you take a look at the Data Mapper pattern, you have the Mapper that accesses both the Model and the database. The Model is passive - usually does not call external resources (it's a POPO - Plain Old PHP Object).

A solution for your issue is to inject the related information into the Model, thus keeping the Model only as a data structure.

Here is a working scenario for an MVC application:

Controller - used for input validation & retrieving data from services

<?php
...
public function viewAction()
{
    $id = (int) $this->params()->fromQuery('id');
    $service = $this->getServiceLocator()->get('your-user-service-name');
    $user = $service->getUser($id);
    ...
}

Service - used for executing the business logic; calls multiple Data Mappers

<?php
...
public function getUser($id)
{
    // get user
    $mapper = $this->getServiceLocator()->get('your-user-mapper');
    $user = $mapper->getUserById($id);

    // get articles
    $article_mapper = $this->getServiceLocator()->get('your-article-mapper');
    $user->articles = $article_mapper->getArticlesByUser($id);

    return $user;
}

Data Mapper - used to manipulate one type of Domain entity - it should be composed with a tableGateway if you are accessing the database

<?php
...
public function getUserById($id)
{
    $select = $this->tableGateway->getSql()->select();
    $select = $select->where(array('id' => $value));
    $row = $this->tableGateway->selectWith($select)->current();
    return $row;
}

Domain Model - used for data representation

<?php
...
class User 
{
    public $name; // user name
    ...
    public $articles; // holds the user articles
}

Advantages

  1. Passive Models are easy to read - understand the data structure and it's relations.
  2. Passive Models are easy to test - you don't need external dependencies.
  3. You separate the persistence layer from the Domain layer.

Hope this helps!