扩展原则实体以添加业务逻辑 [英] Extending Doctrine Entity in order to add business logic

查看:105
本文介绍了扩展原则实体以添加业务逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力练习一个好的设计和延伸的教义实体。
我的扩展类,该模型基本上将具有额外的业务逻辑+访问实体基本数据。



我使用的是Doctrine 2.2.1& Zend Framework 1.11.4& php 5.3.8



当我使用DQL时,doctrine成功返回Model实体。
当我使用Doctrine native find()函数时,它不返回任何内容:(。



帮助...



这是怎么回事:



Bootstrap.php

  $ classLoader = new \Doctrine\Common\ClassLoader('Entities',APPLICATION_PATH。'/ doctrine'); 
$ classLoader-> register();
$ classLoader = new \Doctrine\Common\ClassLoader('Models',APPLICATION_PATH);
$ classLoader-> register();
pre>

APPLICATION_PATH\models\User.php中的模型

 命名空间模型; 
使用Doctrine\ORM\Query;

/ **
* Models\User
*
* @Table(name =user)
* @Entity
* /
类用户扩展\Entities\User {

public函数__wakeup(){
$ this-> tools = new Application_App_Tools();
}

实体检索功能



不工作

  $ userEntity = $ registry-> entityManager-> find('Models\User',$ userEntity); 

WORKS

  $ qry = $ qb 
- > select('u')
- > from('Models\用户, 'U');


解决方案

您不应该将业务逻辑添加到实体您应该使用模型。一种方法是:


  1. 使用商业逻辑模型。

  2. 创建自定义原则2个存储库用于您的所有数据库查询(DQL或其他)[1]。

  3. 将您的实体单独留下。

在实践中,这意味着模型是纯粹的PHP类(或者根据你使用的是什么框架,但是你的模型与你的数据库没有任何关系)。然而,您的模型会实例化您的自定义原则2存储库。例如。 UserRepository 可能包含一个名为 getUserById 的方法。在您的资料库中,您可以运行实际的查询和返回实体以使模型合作。



[1] http://docs.doctrine-project.org/en/latest/reference/working- with-objects.html#custom-repositories


I'm trying to practice a good design and extending Doctrine entity. My extended class, the model basically, will have extra business logic + access to the entity basic data.

I am using Doctrine 2.2.1 & Zend Framework 1.11.4 & php 5.3.8

When I use DQL, doctrine return successfully the Model entity. When I use Doctrine native find() function, it returns nothing :(.

HELP...

This is how it rolls:

Bootstrap.php:

    $classLoader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH.'/doctrine');
    $classLoader->register();
    $classLoader = new \Doctrine\Common\ClassLoader('Models', APPLICATION_PATH);
    $classLoader->register();

Model in APPLICATION_PATH\models\User.php:

namespace Models;
use Doctrine\ORM\Query;

/**
 * Models\User
 *
 * @Table(name="user")
 * @Entity
 */
class User extends \Entities\User {

public function __wakeup() {
    $this->tools = new Application_App_Tools();
}

Entity retrieval functions:

DOESN'T WORK:

$userEntity = $registry->entityManager->find('Models\User', $userEntity);

WORKS:

$qry = $qb
        ->select('u')
        ->from('Models\User','u'); 

解决方案

You shouldn't add the business logic to the entities, you should use models for that instead. One way of doing it would be:

  1. Use models for business logic.
  2. Create custom Doctrine 2 repositories for your all your database queries (DQL or otherwise) [1].
  3. Leave your entities alone.

In practise this means that models are plain PHP classes (or maybe framework extended depending on what your're using) but your models have no relation to your database. Your models do however, instantiate your custom Doctrine 2 repositories. E.g. a UserRepository might contain a method called getUserById. Within your repositories is where your run your actual queries and return entity instances for the models to work with.

[1] http://docs.doctrine-project.org/en/latest/reference/working-with-objects.html#custom-repositories

这篇关于扩展原则实体以添加业务逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆