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