duan7264 2013-09-09 18:52
浏览 62
已采纳

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

  • 写回答

2条回答 默认 最新

  • doushi9856 2013-09-10 08:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器