【问题标题】:Type hinting of returned entity in Doctrine2 repository在 Doctrine2 存储库中返回实体的类型提示
【发布时间】:2015-10-30 12:38:50
【问题描述】:

我有 CustomRepository 扩展 Doctrine\ORM\EntityRepository 的类。该存储库与许多不同的实体相关联。 存储库中有一个返回关联实体的方法。

class CustomRepository extends \Doctrine\ORM\EntityRepository
{
    function getEntity() { ... } // returns an instance of associated entity
}

/**
 * @ORM\Entity(repositoryClass="CustomRepository")
 */
class EntityClass1 { ... }    
/**
 * @ORM\Entity(repositoryClass="CustomRepository")
 */
class EntityClass2 { ... }

$repo1 = $entityManager->getRepository('Entity1');
$entity1 = $repo1->getEntity(); // will return an instance of EntityClass1

$repo2 = $entityManager->getRepository('Entity2');
$entity2 = $repo1->getEntity(); // will return an instance of EntityClass2

我使用 Symfony 2 插件,它可以正确检测继承方法实体返回的类为find
有没有办法通知插件该方法返回关联实体?

【问题讨论】:

    标签: symfony doctrine-orm phpstorm


    【解决方案1】:

    你可以这样使用:

    /**
     * MyEntity repository.
     *
     * @method MyEntity[] find
     */
    

    【讨论】:

    • 很抱歉缺少详细信息。我添加了代码示例来大致描述我要问的内容。
    • 所以你想在你的存储库类的方法上输入提示,对吧?因此,例如 $entity2 被称为 Entity2 类型
    • 可以,但是方法本身返回的是不同类的实体(例子中是EntityClass1,EntityClass2)。但是项目中有很多这样的实体(我对很多实体使用CustomRepository而不是EntityRepository),所以相同的方法可能会返回不同类型的实体...
    • 但是对不同的实体使用相同的存储库并不是一个好的方法,最好创建一个 BaseRepository 并创建 Entity1Repository 和 Entity2Repository 来扩展这个新的。然后按照我在两个新实体存储库中的回答中的建议添加@method 注释。
    猜你喜欢
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多