扩展Doctrine Entity以添加业务逻辑

oha*_*wkn 8 php model-view-controller orm doctrine-orm

我正在努力练习一个好的设计并扩展Doctrine实体.我的扩展类(基本上是模型)将具有额外的业务逻辑+访问实体基本数据.

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

实体检索功能:

不工作:

$userEntity = $registry->entityManager->find('Models\User', $userEntity);
Run Code Online (Sandbox Code Playgroud)

工作:

$qry = $qb
        ->select('u')
        ->from('Models\User','u'); 
Run Code Online (Sandbox Code Playgroud)

Iva*_*jak 1

据我了解,DoctrineentityManager只负责持久实体,并且扩展Entities\User实体Model\User将创建另一个实体(存储在 docblock 中所述的同一个表中),但不由entityManager它管理或与之冲突,因为您可能没有@InheritanceType("SINGLE_TABLE")Entities\Userdocblocks 中提到:

阅读此文档以获取更多信息http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html

  • 那么您希望在 Entities\User 中拥有所有存储/增删逻辑,在 Models\User 中拥有所有业务逻辑吗?在这种情况下,没有理由让 DoctrineEntityManager 处理你的模型,因为它显然不是一个实体。让你的模型使用实体而不是像这样扩展它: $model = new Models\User($entity); $模型->doSomeBusinessLogic(); (2认同)