将我添加到无法弄清楚其学说映射的人列表中。我正在用Onetomany Halfmoves
建模国际象棋Game
-有什么想法?
ddl:
create table game ( game_id int primary key );
create table halfmove(halfmove_id int primary key, game_id int);
game.php:
/**
* Game
*
* @ORMTable(name="game")
* @ORMEntity
*/
class Game
{
/**
* @ORMOneToMany(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
*
* @ORMTable(name="halfmove")
* @ORMEntity
*/
class Halfmove
{
/**
* @var integer
*
* @ORMColumn(name="game_id", type="integer", nullable=true)
*/
private $gameId;
/**
* @ORMManyToOne(targetEntity="Game", inversedBy="halfmoves")
* @ORMJoinColumn(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]请求。 学说 orm query queryException:" [语义错误]第0行,col 62 接近'c':错误:类AppBundle Entity HalfMove没有 协会命名为游戏" AT /users/cats/sites/chess-ui/vendor/doctrine/orm/lib/doctrine/query/query/query/queryexception.php 第63行{"异常":" [对象] (Doctrine orm query queryException(代码:0):[语义错误] 第0行,col 62附近'c n':错误:类 AppBundle Entity HalfMove在 /users/cats/sites/chess-ui/vendor/doctrine/orm/lib/doctrine/query/query/queryexception.php:63, 学说 orm query queryException(代码:0):从 AppBundle:HalfMove P n JOIN加入P.Game C n在哪里 c.game_id =:id at /users/cats/sites/chess-ui/vendor/doctrine/orm/lib/doctrine/query/query/queryexception.php:41) []
您在 Halfmove
entity中声明了 $game
。用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 DoctrineORMMapping as ORM;
/**
* Game
*
* @ORMTable(name="game")
* @ORMEntity
*/
class Game
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMOneToMany(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 DoctrineORMMapping as ORM;
/**
* Halfmove
*
* @ORMTable(name="halfmove")
* @ORMEntity
*/
class Halfmove
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="Game", inversedBy="halfmoves")
* @ORMJoinColumn(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();
尝试这个:
/**
* @ORMManyToOne(targetEntity="Game", inversedBy="halfmoves")
* @ORMJoinColumn(name="game_id", referencedColumnName="id")
*/
private $game;
将game_id更改为引用的columnname中的id。
我的情况下的问题是,映射信息根本不是来自实体注释 - 它是从XML映射中读取的,这些映射是作为反向工程的一部分而创建的(来自DB) 过程。一旦删除这些内容,就使用注释并按照预期的方式工作。