Error when targetEntity is in a different bundle
doctrine-orm, symfony
Solution
i've successfully used cross bundle entity relations without any problems. if you have automatic mapping disabled you need to tell doctrine each bundle that contains entities.
so in app/config.yml you need to have either this:
doctrine:
orm:
auto_mapping: true
or this:
doctrine:
orm:
auto_mapping: false
entity_managers:
default:
mappings:
VNNCoreBundle: ~
VNNPressboxBundle: ~
check your database if tables for the bundles exist. also you could try running `./app/console doctrine:generate:entities --force` and make sure it runs without errors
edit
i checked my code and the leading `/` is not necessary. also the `use` statement is superfluous.
Problem
I'm getting the following error when I try to do `./app/console doctrine:migrate:diff`: ``` [Doctrine\ORM\Mapping\MappingException] Class VNN\CoreBundle\Entity\Game is not a valid entity or mapped super class. ``` Here's the line that's causing it: ``` /** * @ORM\ManyToOne(targetEntity="VNN\CoreBundle\Entity\Game") ``` Here's the relevant part of my class: ``` <?php namespace VNN\PressboxBundle\Entity; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Query\Expr\OrderBy; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Type; use VNN\CoreBundle\Entity\Game; /** * VNN\PressboxBundle\Entity\Event * * @ORM\Table(name="event") * @ORM\Entity */ class Event { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\ManyToOne(targetEntity="VNN\CoreBundle\Entity\Game") * @ORM\JoinColumn(name="core_game_id", referencedColumnName="game_id") **/ private $coreGame; ``` What I understand it's saying is not that it can't find `VNN\CoreBundle\Entity\Game`, just that `Game` is not a valid entity. So here's `Game.php`: ``` <?php namespace VNN\CoreBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * VNN\CoreBundle\Entity\Game * * @ORM\Table(name="games") * @ORM\Entity */ class Game { /** * @var integer $id * * @ORM\Column(name="game_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer $homeSchoolId * * @ORM\Column(name="home_school_id", type="integer") */ private $homeSchoolId; /** * @var integer $awaySchoolId * * @ORM\Column(name="away_school_id", type="integer") */ private $awaySchoolId; /** * @ORM\ManyToOne(targetEntity="School") * @ORM\JoinColumn(name="home_school_id", referencedColumnName="school_id") **/ private $homeSchool; /** * @ORM\ManyToOne(targetEntity="School") * @ORM\JoinColumn(name="away_school_id", referencedColumnName="school_id") **/ private $awaySchool; /** * @ORM\ManyToOne(targetEntity="Sport") * @ORM\JoinColumn(name="sport_id", referencedColumnName="sport_id") **/ private $sport; /** * @var integer $datetime * * @ORM\Column(name="game_datetime") */ private $datetime; /** * @var integer $homeScore * * @ORM\Column(name="home_score", type="integer") */ private $homeScore; /** * @var integer $awayScore * * @ORM\Column(name="away_score", type="integer") */ private $awayScore; public function getId() { return $this->id; } public function getHomeSchool() { if ($this->homeSchoolId > 0) { return $this->homeSchool; } else { return FALSE; } } public function getAwaySchool() { if ($this->awaySchoolId > 0) { return $this->awaySchool; } else { return FALSE; } } public function recordIsValid() { if (!($this->homeSchoolId > 0)) { return FALSE; } if (!($this->awaySchoolId > 0)) { return FALSE; } return TRUE; } public function getSport() { return $this->sport; } public function getHumanDatetime() { $date = new \DateTime($this->datetime); return $date->format('F d, Y g:ia'); } public function getDatetime() { $date = new \DateTime($this->datetime); return $date->format('m/d/Y g:i:s a'); } public function getOpposingSchoolWithRespectToSchoolName($schoolName) { if ($schoolName == $this->getHomeSchool()->getName()) { return $this->getAwaySchool(); } else { return $this->getHomeSchool(); } } public function getHomeScore() { return $this->homeScore; } public function getAwayScore() { return $this->awayScore; } } ``` Why would it not like `Game`? Update: I had this same exact problem again when I tried to do the same thing with another cross-bundle entity. I found this post, but the add-a-leading-slash fix didn't do it for me like it evidently did for the OP there.