donglisi8644 2014-08-24 21:59
浏览 41
已采纳

Symfony2 + Doctrine Association错误:类游戏没有名为home的关联

Here are the columns and rows of the MySQL tables: enter image description here

I'm trying to inner join the game.home and game.away with the Team.id, but i'm getting the error: "Error: Class Travel\Bundle\TravelBundle\Entity\Game has no association named home" what am I doing incorrectly?

Here is the desired results I want to achieve, I know how to do it in MySQL, but not Symfony and Doctrine:

select 
    t1.id as gameid, 
    t1.season as gameseason, 
    t1.gametype as gametype, 
        t2.id as homeid, 
        t2.name as homename, 
        t2.abbreviation as homeabbreviation, 
        t2.type as hometype, 
        t2.image as homeimage,
            t3.id as awayid, 
            t3.name as awayname, 
            t3.abbreviation as awayabbreviation, 
            t3.type as awaytype, 
            t3.image as awayimage from Game t1 
            INNER JOIN Team t2 on t1.home  = t2.id
            INNER JOIN Team t3 on t1.away  = t3.id;

Here are my Doctrine entities:

Game.orm.yml:

Travel\Bundle\TravelBundle\Entity\Game:
    type: entity
     OneToOne:
        targetEntity: Travel\Bundle\TravelBundle\Entity\Team
    JoinColumn:
        name: home
        referencedColumnName: id
    repositoryClass: Travel\Bundle\TravelBundle\Entity\GameRepository
    fields:
        id:
            type: integer
            id: true
        generator:
            strategy: AUTO
        home:
            type: integer  
        away:
            type: integer
        season:
            type: string
        gametype:
        type: integer
        username:
        type: string
    lifecycleCallbacks: {  }

Team.orm.yml

Travel\Bundle\TravelBundle\Entity\Team:
    type: entity
    table: null
    repositoryClass: Travel\Bundle\TravelBundle\Entity\TeamRepository
    fields:
        id:
            type: integer
            id: true
            generator:
            strategy: AUTO
        name:
            type: string
            length: 255
        abbreviation:
            type: string
            length: 255
        type:
            type: string
            length: 255
        name:
            type: string
            length: 255
        image:
            type: string
            length: 255
        username:
            type: string
            length: 255
    lifecycleCallbacks:
        prePersist: [ uploadImage ]
        prePersist: [ preUpload ]
        preUpdate: [ preUpload ]
        postPersist: [ moveImage ]
        preRemove: [ removeImage ]

GameRepository.php

<?php

namespace Travel\Bundle\TravelBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * GameRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class GameRepository extends EntityRepository
{
    public function allByUsername($username)
    {
        $query = $this->getEntityManager()
        ->getRepository('TravelTravelBundle:Game')
        ->createQueryBuilder('t')
        ->select('t','g')
        ->innerJoin('t.home', 'g')
        ->setParameter('username', $username);
        $token = $query->getQuery()->getResult();
        return $token;
    }
}

?>
  • 写回答

1条回答 默认 最新

  • dqqn32019 2014-08-25 06:30
    关注

    According to doctrine documentation your mapping should look like:

    Game.orm.yml:

    Travel\Bundle\TravelBundle\Entity\Game:
        type: entity
        repositoryClass: Travel\Bundle\TravelBundle\Entity\GameRepository
        oneToOne:
            home:
                targetEntity: Travel\Bundle\TravelBundle\Entity\Team
                joinColumn:
                    name: home
                    referencedColumnName: id
            away:
                targetEntity: Travel\Bundle\TravelBundle\Entity\Team
                joinColumn:
                    name: away
                    referencedColumnName: id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c