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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。