本文介绍了教义“没有关联命名"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将我添加到无法确定其教义映射出什么问题的人员列表中.我正在用OneToMany Halfmoves
为国际象棋Game
建模-有什么想法吗?
Add me to the list of people who can't work out what's wrong with their Doctrine mapping. I'm modelling a Chess Game
with OneToMany Halfmoves
- any ideas?
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.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();
错误消息:
推荐答案
您在Halfmove
实体中声明了$game
.在用p.game
替换p.Game
之后尝试.
You declared $game
in Halfmove
entity. Try after replacing p.Game
with 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();
其他更新
此外,我建议您对实体进行一些更改.
Additionally i recommend some changes in you entities.
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();
这篇关于教义“没有关联命名"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!