I have an Entity called Game
with a related Repository called GameRepository
:
/**
* @ORM\\Entity(repositoryClass=\"...\\GameReposi
You actually can get the repository in your entity and only during a lifecycle callback. You are very close to it, all you have to do is to receive the LifecycleEventArgs
parameter.
Also see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* @ORM\Entity(repositoryClass="...\GameRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Game {
/**
* @ORM\prePersist
*/
public function setSlugValue( LifecycleEventArgs $event ) {
$entityManager = $event->getEntityManager();
$repository = $entityManager->getRepository( get_class($this) );
$this->slug = $repository->createUniqueSlugForGame();
}
}
PS. I know this is an old question, but I answered it to help any future googlers.