当targetEntity位于不同的包中时出错

时间:2012-04-29 12:19:17

标签: symfony doctrine-orm

当我尝试./app/console doctrine:migrate:diff时,我收到以下错误:

 [Doctrine\ORM\Mapping\MappingException]                                        
  Class VNN\CoreBundle\Entity\Game is not a valid entity or mapped super class. 

以下是引起它的行:

/**
 * @ORM\ManyToOne(targetEntity="VNN\CoreBundle\Entity\Game")

以下是我班级的相关部分:

<?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;

我理解的并不是说它不能找到 VNN\CoreBundle\Entity\Game,只是Game不是有效的实体。所以这里是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;
    }
}

为什么不喜欢Game

更新:当我尝试与另一个交叉捆绑实体做同样的事情时,我又遇到了同样的问题。我找到了this post,但是添加一个领先的斜线修复程序对我来说没有这样做,就像那里显然那样。

2 个答案:

答案 0 :(得分:14)

我已成功使用交叉捆绑实体关系而没有任何问题。如果禁用了自动映射,则需要告诉每个包含实体的包的学说。

所以在app / config.yml中你需要有这个:

doctrine:
    orm:
        auto_mapping: true

或者这个:

doctrine:
    orm:
        auto_mapping: false
        entity_managers:
            default:
                mappings:
                    VNNCoreBundle: ~
                    VNNPressboxBundle: ~

检查数据库是否存在捆绑包的表。你也可以尝试运行./app/console doctrine:generate:entities --force并确保它运行没有错误

修改

我检查了我的代码,并且没有必要使用前导/use声明也是多余的。

答案 1 :(得分:2)

我能够在我的捆绑包中使用这样的东西。

/**
 * @ORM\ManyToOne(targetEntity="\Company\SomeBundle\Entity\Game")
 */

您不需要use \Company\SomeBundle\Entity\Game声明。

此外,请确保在AppKernel.php文件中启用了两个捆绑包。