如何扩展Doctrine2的实体存储库构造函数?

时间:2014-01-27 08:13:25

标签: php symfony constructor doctrine-orm

在我的实体管理器中重写构造函数时,这不起作用:

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{

    function __construct()
    {
        parent::__construct();
        $this->CI =& get_instance();
    }

    public function getUserFromKey()
    { 
// Rest of the function/class ..

为什么? (我从Doctrine ORM核心获得了大量的错误)

1 个答案:

答案 0 :(得分:5)

Doctrine EntityRepository在构造函数中有一些参数,你需要复制它并过去到父构造函数

/**
 * Initializes a new <tt>EntityRepository</tt>.
 *
 * @param EntityManager         $em    The EntityManager to use.
 * @param Mapping\ClassMetadata $class The class descriptor.
 */
public function __construct($em, Mapping\ClassMetadata $class)
{
    $this->_entityName = $class->name;
    $this->_em         = $em;
    $this->_class      = $class;
}


例如

class UserRepository extends EntityRepository
{
    function __construct($em, Mapping\ClassMetadata $class)
    {
        parent::__construct($em, $class);
        $this->CI =& get_instance(); // looks strange
    }