简体   繁体   English

抽象存储库和@Entity批注继承

[英]Abstract repository and @Entity annotation inheritance

I implement a custom EntityRepository in a Symfony2 application. 我在Symfony2应用程序中实现了自定义EntityRepository

I have an AbstractEntity (used as parent of some entities) : 我有一个AbstractEntity(用作某些实体的父代):

/**
 * Abstract Entity.
 *
 * @ORM\Entity(repositoryClass="App\Util\Entity\AbstractRepository")
 */
abstract class AbstractEntity
{
    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    // ...
}

The custom EntityRepository : 自定义EntityRepository

/**
 * Abstract Repository.
 */
class AbstractRepository extends Doctrine\ORM\EntityRepository
{
    /**
     * Handles resource not found from find.
     *
     * @param int $id
     *
     * @return object
     */
    public function findOrFail($id)
    {
        if (null == $entity = $this->find($id)) {
            return $this->fail();
        }

        return $entity;
    }

   //...
}

And one of the entities that extends from AbstractEntity : 以及从AbstractEntity扩展出来的实体之一:

/**
 * @ORM\Table(name="tags_sport")
 */
class Tag extends AbstractEntity
{
     // ...
}

The custom repository is declared once in my AbstractEntity using @ORM\\Entity annotation. 自定义存储库使用@ORM\\Entity批注在我的AbstractEntity中声明一次。
I want make my entities use the same repository without redeclare it. 我想让我的实体使用相同的存储库而不重新声明它。

But an error occurs : 但是发生错误:

Class "App\SportBundle\Entity\Tag" sub class of "App\Util\Entity\AbstractEntity" is not a valid entity or mapped super class

It's caused by the missing @ORM\\Entity annotation in the child entity. 这是由@ORM\\Entity中缺少@ORM\\Entity注释引起的。

But nothing change. 但是什么都没有改变。
If I add the @ORM\\Entity annotation in the child entity, it will use the default EntityRepository . 如果我在@ORM\\Entity添加@ORM\\Entity批注,它将使用默认的EntityRepository

How can I override the default EntityRepository for each entities extending from the AbstractEntity ? 如何覆盖从AbstractEntity扩展的每个实体的默认EntityRepository

EDIT I accept @Rvanlaak answer thanks to a gist given in the answer comments. 编辑我接受@Rvanlaak的回答,这要归功于答案注释中给出的要点。
The final solution including the whole custom Repository in this gist 最终解决方案包括本要点中的整个自定义存储库

These are two separate things, having an abstract parent entity, and having an abstract parent repository. 这是两个独立的事物,具有一个抽象的父实体和一个抽象的父存储库。

By default every entity will use the EntityRepository . 默认情况下,每个实体都将使用EntityRepository The entity can have one repository, which can be defined via the ORM\\Entity annotation: http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes 实体可以有一个存储库,可以通过ORM\\Entity批注定义: http : //symfony.com/doc/current/book/doctrine.html#custom-repository-classes

Let's say you create TagRepository yourself, there will be no problem with extending from your AbstractRepository . 假设您自己创建TagRepository ,那么从AbstractRepository扩展将没有问题。

Second, having an abstract entity. 第二,具有抽象实体。 This is like telling Doctrine that your entity has a parent class. 这就像告诉Doctrine您的实体有一个父类。 This isn't possible. 这是不可能的。 The solution for that is to use traits . 解决的办法是使用traits Found a nice article about that here: http://www.sitepoint.com/using-traits-doctrine-entities/ 在此处找到了一篇不错的文章: http : //www.sitepoint.com/using-traits-doctrine-entities/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM