Doctrine"没有名为"

时间:2016-11-28 12:55:04

标签: php symfony orm doctrine-orm doctrine

将我添加到无法解决他们的学说映射错误的人员列表中。我使用OneToMany Game为国际象棋Halfmoves建模 - 任何想法?

DDL:

create table game ( game_id int primary key  ); 
create table halfmove(halfmove_id int primary key, game_id int);

Game.php:

/**
* Game
*
* @ORM\Table(name="game")
* @ORM\Entity
*/
class Game  
{
/**
 * @ORM\OneToMany(targetEntity="Halfmove", mappedBy="game")
 */
private $halfmoves;

public function getHalfmoves(){
    return $this->halfmoves;
}

public function setHalfmoves($halfmoves){
    $this->$halfmoves = $halfmoves;
}

public function __construct()
{
    $this->halfmoves = new ArrayCollection();
}
...

Halfmove.php:

/**
 * Halfmove
 *
 * @ORM\Table(name="halfmove")
 * @ORM\Entity
 */
class Halfmove
{
/**
 * @var integer
 *
 * @ORM\Column(name="game_id", type="integer", nullable=true)
 */
private $gameId;


/**
 * @ORM\ManyToOne(targetEntity="Game", inversedBy="halfmoves")
 * @ORM\JoinColumn(name="game_id", referencedColumnName="game_id")
 */
private $game;

public function getGame(){
    return $this->game;
}

public function setGame($game){
    $this->game = $game;
}
...

生成错误的查询:

    $em = $this->getDoctrine()->getManager();
    $query = $em
    ->createQuery(
            'SELECT p, c FROM AppBundle:Halfmove p
        JOIN p.Game c
        WHERE c.game_id = :id'
            )->setParameter('id', 3525);
    $result =  $query->getSingleResult();

错误讯息:

  

2016-11-28 12:46:46] request.CRITICAL:未捕获PHP异常   Doctrine \ ORM \ Query \ QueryException:" [语义错误]第0行,第62行   接近' c':错误:类AppBundle \ Entity \ Halfmove没有   协会名为Game"在   /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php   第63行{"例外":" [对象]   (Doctrine \ ORM \ Query \ QueryException(代码:0):[语义错误]   第0行,第62行附近' c \ n':错误:类   AppBundle \ Entity \ Halfmove没有名为Game at的关联   /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:63,   Doctrine \ ORM \ Query \ QueryException(代码:0):SELECT p,c FROM   AppBundle:Halfmove p \ n JOIN p.Game c \ n WHERE   c.game_id =:id at   /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:41)"}   []

3 个答案:

答案 0 :(得分:1)

您在$game实体中声明了Halfmove。用p.Game替换p.game后尝试。

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
        'SELECT p, c FROM AppBundle:Halfmove p
            JOIN p.game c
            WHERE c.game_id = :id'
        )->setParameter('id', 3525);
$result =  $query->getSingleResult();

其他更新

此外,我建议您对实体进行一些更改。

<强> Game.php

<?php

use Doctrine\ORM\Mapping as ORM;

/**
* Game
*
* @ORM\Table(name="game")
* @ORM\Entity
*/
class Game  
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Halfmove", mappedBy="game")
     */
    private $halfmoves;

    public function getHalfmoves(){
        return $this->halfmoves;
    }

    public function setHalfmoves($halfmoves){
        $this->$halfmoves = $halfmoves;
    }

    public function __construct()
    {
        $this->halfmoves = new ArrayCollection();
    }
    ...

<强> Halfmove.php

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * Halfmove
 *
 * @ORM\Table(name="halfmove")
 * @ORM\Entity
 */
class Halfmove
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Game", inversedBy="halfmoves")
     * @ORM\JoinColumn(name="game_id", referencedColumnName="id")
     */
    private $game;

    public function getGame(){
        return $this->game;
    }

    public function setGame($game){
        $this->game = $game;
    }
    ...

执行查询

$query = $em->getRepository("Halfmove")
    ->createQueryBuilder('p')
    ->innerJoin("p.game", 'c')
    ->where("c.id = :CId")
    ->setParameter("CId", 3525);

$moves = $query->getQuery()->getResult();

答案 1 :(得分:-1)

试试这个:

/**
 * @ORM\ManyToOne(targetEntity="Game", inversedBy="halfmoves")
 * @ORM\JoinColumn(name="game_id", referencedColumnName="id")
 */
private $game;

在referencedColumnName中将game_id更改为id。

答案 2 :(得分:-1)

我的问题是,映射信息根本不是来自实体Annotations - 它是从作为逆向工程(来自DB)过程的一部分创建的Xml映射中读取的。删除这些后,使用了注释,一切都按预期工作。