douji8017 2016-11-28 12:55
浏览 40

学说“没有任何关联”

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
 *
 * @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;
}
...

Query that generates error:

    $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();

Error message:

2016-11-28 12:46:46] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 62 near 'c ': Error: Class AppBundle\Entity\Halfmove has no association named Game" at /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 63 {"exception":"[object] (Doctrine\ORM\Query\QueryException(code: 0): [Semantical Error] line 0, col 62 near 'c ': Error: Class AppBundle\Entity\Halfmove has no association named Game at /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:63, Doctrine\ORM\Query\QueryException(code: 0): SELECT p, c FROM AppBundle:Halfmove p JOIN p.Game c WHERE c.game_id = :id at /Users/cats/Sites/chess-ui/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:41)"} []

  • 写回答

3条回答 默认 最新

  • dozr162106 2016-11-28 13:21
    关注

    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();
    

    Additional Update

    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;
        }
        ...
    

    Execute Query

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

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错