7

尝试执行以下操作时出现以下错误./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

更新:当我尝试对另一个跨捆绑实体做同样的事情时,我又遇到了同样的问题。我找到了这篇文章,但是 add-a-leading-slash 修复并没有像它显然对那里的 OP 那样为我做。

4

2 回答 2

14

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.

于 2012-06-11T23:41:07.033 回答
2

我能够在我的捆绑包中得到这样的东西。

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

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

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

于 2012-06-11T23:32:55.383 回答