抽象库和@Entity注释继承 [英] Abstract repository and @Entity annotation inheritance

查看:108
本文介绍了抽象库和@Entity注释继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony2应用程序中实现一个定制的 EntityRepository



我有一个AbstractEntity一些实体):

  / ** 
*抽象实体。
*
* @ ORM\Entity(repositoryClass =App \Util\Entity\AbstractRepository)
* /
抽象类AbstractEntity
{
/ **
*获取ID。
*
* @return int
* /
public function getId()
{
return $ this-> id;
}

// ...
}

定制 EntityRepository

  / ** 
*抽象库。
* /
class AbstractRepository扩展了Doctrine\ORM\EntityRepository
{
/ **
*处理从find找不到的资源。
*
* @param int $ id
*
* @return对象
* /
public function findOrFail($ id)
{
if(null == $ entity = $ this-> find($ id)){
return $ this-> fail();
}

return $ entity;
}

// ...
}

AbstractEntity 中扩展的实体之一:

  ** 
* @ ORM\Table(name =tags_sport)
* /
class标签扩展AbstractEntity
{
// ...

自定义存储库在我的AbstractEntity中使用 @ ORM\\ \\ Entity 注释。

我想让我的实体使用相同的存储库,而无需重新声明。



但是发生错误:

 类App \SportBundle\Entity\Tag子类App \Util\Entity\\ \\AbstractEntity不是有效的实体或映射的超级类

它是由缺少的 @ ORM\Entity 子实体中的注释。



但没有变化。

如果我添加了 @ ORM\Entity 注释子实体,它将使用默认的 EntityRepository



如何覆盖默认的 EntityRepository AbstractEntity



EDIT 我接受@Rvanlaak的回答,感谢答复评论中给出的要点。

最终解决方案包括整个定制存储库在这个要点

解决方案

这是两个不同的东西,实体,并具有抽象父存储库。



默认情况下,每个实体将使用 EntityRepository 。实体可以有一个存储库,可以通过 ORM\Entity 注释来定义: http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes



假设您自己创建 TagRepository ,从 AbstractRepository



其次,具有抽象实体。这就像告诉Doctrine你的实体有一个父类。这是不可能的。解决方案是使用 traits 。在这里找到一篇很好的文章: http://www.sitepoint.com/using- traits-doctrine-entities /


I implement a custom EntityRepository in a Symfony2 application.

I have an AbstractEntity (used as parent of some entities) :

/**
 * 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 :

/**
 * 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 :

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

The custom repository is declared once in my AbstractEntity using @ORM\Entity annotation.
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.

But nothing change.
If I add the @ORM\Entity annotation in the child entity, it will use the default EntityRepository.

How can I override the default EntityRepository for each entities extending from the AbstractEntity ?

EDIT I accept @Rvanlaak answer thanks to a gist given in the answer comments.
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. 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

Let's say you create TagRepository yourself, there will be no problem with extending from your AbstractRepository.

Second, having an abstract entity. This is like telling Doctrine that your entity has a parent class. This isn't possible. The solution for that is to use traits. Found a nice article about that here: http://www.sitepoint.com/using-traits-doctrine-entities/

这篇关于抽象库和@Entity注释继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆