调用实体存储库时,Doctrine2“类不存在”错误

时间:2012-12-18 16:47:00

标签: annotations doctrine-orm many-to-many many-to-one

我对doctrine 2的注释系统有一些问题(我使用2.3集成到Zend框架2中)。在相对简单的关系上我没有问题,但是我有很多级联表,当我调用存储库查找方法时,我得到一个类不存在错误:

doctrine\orm\lib\Doctrine\ORM\Proxy\ProxyFactory.php:233

数据库架构本身可能存在一些问题,但是我无法更改它,我必须从这个数据库中创建我的应用程序。

我的所有实体都以:

开头
namespace Gcl\Entities;
use Doctrine\ORM\Mapping as ORM;

以下是我的一些实体:

TCompanyGcl

    /**
     * TCompanyGcl
     *
     * @ORM\Table(name="T_COMPANY_GCL")
     * @ORM\Entity(repositoryClass="Gcl\Repositories\TCompanyGclRepository")
     */
    class TCompanyGcl
    {
           /**
             * @var ArrayCollection $customerAccounts
             *
             * @ORM\OneToMany(targetEntity="TCustomerAccount", mappedBy="tCompanyGcl")
             */
            private $customerAccounts;

TCustomerAccount

/**
 * TCustomerAccount
 *
 * @ORM\Table(name="T_CUSTOMER_ACCOUNT")
 * @ORM\Entity
 */
class TCustomerAccount
{
   /**
     * @var \TCompanyGcl
     *
     * @ORM\ManyToOne(targetEntity="TCompanyGcl", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="COMPANY_ID", referencedColumnName="COMPANY_ID")
     * })
     */
    private $tCompanyGcl;

    /**
     * @var \TPerson
     *
     * @ORM\ManyToOne(targetEntity="TPerson", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="PERSON_ID", referencedColumnName="PERSON_ID")
     * })
     */
    private $person;

    /**
     * @var ArrayCollection $customerSubscriptions
     *
     * @ORM\OneToMany(targetEntity="TSubscription", mappedBy="customerAccount")
     */
    private $customerSubscriptions;

TSubscriptions

/**
 * TSubscription
 *
 * @ORM\Table(name="T_SUBSCRIPTION")
 * @ORM\Entity(repositoryClass="Gcl\Repositories\TSubscriptionRepository")
 */
class TSubscription
{
   /**
     * @var \TCustomerAccount
     *
     * @ORM\ManyToOne(targetEntity="TCustomerAccount", inversedBy="customerSubscriptions")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="CUSTOMER_ACCOUNT_ID", referencedColumnName="CUSTOMER_ACCOUNT_ID")
     * })
     */
    private $customerAccount;

所以当我做的事情就是问题:

$account = $em->getRepository('Gcl\Entities\TCustomerAccount')->find(1);
$account->getSubscriptions();
$account->getCompanyGcl();

它可以工作,或者当我从TCompanyGcl一直到订阅时,它也可以工作。 但是当我这样做时:

$subscription = $em->getRepository('Gcl\Entities\TSubscription')->find(1);

我收到上述错误。

如果有人在这个问题上有一点点的领导,我会非常感谢你的帮助。

编辑: 这是存储库: TCompanyGclRepositories

namespace Gcl\Repositories;

use Doctrine\ORM\EntityRepository;

class TCompanyGclRepository extends EntityRepository
{
    /**
     * Récupère la liste des sociétés dont le champ passé en paramètre contient la chaîne de caractères passée en paramètre
     *
     * @param string field nom du champ dans lequel chercher
     * @param string search chaîne de recherche
     * @return Array company
     */
    public function getCompaniesByField($field = 'companyName', $search = '', $return_mode = 'array'){
        $qb = $this->_em->createQueryBuilder();
        $qb->select('c')
        ->from('Gcl\Entities\TCompanyGcl',  'c')
        ->where('UPPER(c.'.$field.') LIKE :search')
        ->setParameter('search', "%".mb_strtoupper($search,'UTF-8')."%");

        if($return_mode=="array"){
            $results = $qb->getQuery()->getArrayResult();
            $companies = array(0 => "Pas de société");

            foreach($results as $result){
                $companies[$result['companyId']] = $result;
            }
        }
        else{
            $companies = $qb->getQuery()->getResult();
        }

        return $companies;
    }

    public function getCustomerAccountPerson($companyId){
        $qb = $this->_em->createQueryBuilder();
        $qb->select('cust, p, t')
                    ->from('Gcl\Entities\TCustomerAccount', 'cust')
                    ->leftJoin('cust.person', 'p')
                    ->leftJoin('p.typeofcivility', 't')
                    ->where('cust.companyId = :id')
                    ->setParameter('id', $companyId);
        return $qb->getQuery()->getResult();

    }
}

TSubscriptionRepositories

namespace Gcl\Repositories;

use Doctrine\ORM\EntityRepository;

class TSubscriptionRepository extends EntityRepository
{
    public function getContractTickets($subscription_id){
        $qb = $this->_em->createQueryBuilder();

        if(!is_array($subscription_id)){
            $subscription_id = array($subscription_id);
        }
        $qb->select('s, t, i, s.itemId, s.subscriptionId, i.printName, t.ticketSerialNumber')
        ->from('Gcl\Entities\TSubscription', 's')
        ->innerJoin('s.ticketSerialNumber', 't')
        ->leftJoin('s.item', 'i')
        ->add('where', $qb->expr()->in('s.subscriptionId', $subscription_id));
        return $qb->getQuery()->getArrayResult();
    }

    public function getContractVehicles($subscription_id){
        $qb = $this->_em->createQueryBuilder();

        if(!is_array($subscription_id)){
            $subscription_id = array($subscription_id);
        }
        $qb->select('s, v, tv, s.itemId, i.printName, v.licencePlate, tv.name')
        ->from('Gcl\Entities\TSubscription', 's')
        ->innerJoin('s.licencePlate', 'v')
        ->leftJoin('v.typeOfVehicle', 'tv')
        ->leftJoin('s.item', 'i')
        ->add('where', $qb->expr()->in('s.subscriptionId', $subscription_id));
        return $qb->getQuery()->getArrayResult();
    }
}

我的实体位于Gcl / Entities文件夹中,我的存储库位于Gcl / Repositories中。当我从entites中删除一些关联时,我可以调用它们并且它们的方法就好了。 但是,如果在订阅存储库中调用find方法时,我从TCustomerAccount中删除了tCompanyGcl和person关联,我收到此错误:

Class Gcl\Entities\ArrayCollection does not exist

2 个答案:

答案 0 :(得分:2)

我发现了问题,不敢相信它在我眼前是如此简单。删除注释的事实是删除错误导致我认为源是注释,但问题来自setter方法。

例如TCustomer上的getCompanyGcl方法:

/**
 * Set companyGcl
 *
 * @param \TCompanyGcl $companyGcl
 * @return TCustomerAccount
 */
public function setCompanyGcl(\TCompanyGcl $companyGcl = null)
{
    $this->companyGcl = $companyGcl;

    return $this;
}

但要工作,你必须在参数中精确定义对象的命名空间,如下所示:

/**
 * Set companyGcl
 *
 * @param \TCompanyGcl $companyGcl
 * @return TCustomerAccount
 */
public function setCompanyGcl(\Gcl\Entities\TCompanyGcl $companyGcl = null)
{
    $this->companyGcl = $companyGcl;

    return $this;
}

答案 1 :(得分:1)

未指定您的TclCustomerAccount存储库,这意味着您使用的是默认值。这应该完美无瑕。您不必提及是否可以使用TCompanyGcl中的自定义存储库。

我猜你自定义存储库的路径是错误的 OR 存储库文件本身的存储库类名拼写错误(或者与注释不同)。

如果您需要更多帮助,您必须自己发布存储库文件。

编辑:

我对注释很弱,我不会使用它们。但是,我认为

@var ArrayCollection $customerSubscriptions 

结合
namespace Gcl\Repositories; 

产生此错误。通过以下方式在注释中指定整个ArrayCollection路径:

use Doctrine\Common\Collection\ArrayCollection as ArrayCollection; 

在文件的头部或每个注释中单独作为

@var Doctrine\Common\Collection\ArrayCollection $customerSubscriptions 

和类似的