首页 > 解决方案 > 使用 getRepository 找不到特定的自定义存储库,但其他的工作正常

问题描述

在我的应用程序中,我有几个自定义存储库,它们与各自的实体类和 orm.yml 文件映射。

它们都以相同的方式定义,当然它们的名称和成员也不同。

然而,当我尝试获取自定义存储库时,仅针对特定存储库,我得到了存储库的超类,而不是正确的存储库。

$a = $this->em->getRepository('MyAppCommonBundle:Activity'); //doesn't work

$b = $this->em->getRepository('MyAppCommonBundle:Activity:User'); //works

$c= $this->em->getRepository('MyAppCommonBundle:ActivityStatus'); //works

MyAppCommonBundle 定义为:MyAppCommonBundle' => 'MyApp\CommonBundle\Entity

$a 返回错误的超类 Doctrine\ORM\EntityRepository 而 $b 返回正确的 MyApp\CommonBundle\Repository\UserRepository 并且 $c 是 MyApp\CommonBundle\Repository\ActivityStatus

所有实体都在同一个文件夹 MyApp\CommonBundle\Entity 中。所有存储库也位于同一位置 MyApp\CommonBundle\Repository。

活动.orm.yml:

MyApp\CommonBundle\Entity\Activity:
    type: entity
    table: activity
    repositoryClass: MyApp\CommonBundle\Repository\ActivityRepository
    indexes:

例如 User.orm.yml:

MyApp\CommonBundle\Entity\User:
    type: entity
    table: user
    repositoryClass: MyApp\CommonBundle\Repository\UserRepository

活动资​​料库:

<?php

namespace MyApp\CommonBundle\Repository;

use Doctrine\ORM\EntityRepository; 


class ActivityRepository extends EntityRepository
{

用户存储库:

<?php

namespace MyApp\CommonBundle\Repository;

use Doctrine\ORM\EntityRepository; 
class UserRepository extends EntityRepository
...

如上所述,找到了 User 和其他存储库,Activity 不是...

我尝试清除缓存,重建没有效果的实体。显然试图寻找错别字等,但看起来不错。

任何想法?谢谢

标签: phpsymfonydoctrine-orm

解决方案


Cerad,没有相关的 ORM 注释。活动实体:

<?php

namespace MyApp\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Activity
 *
 * @ORM\Table(name="activity"...)
 * @ORM\Entity
 */
class Activity
{...

用户实体:

<?php

namespace MyApp\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user"...)
 * @ORM\Entity
 */
class User
{
....

根本没有 orm.xml 文件。弗兰克,是的,存储库类名与其文件名相同。


推荐阅读