php - Symfony2 + Doctrine 关联错误 : Class Game has no association named home

标签 php mysql symfony doctrine-orm doctrine

以下是 MySQL 表的列和行: enter image description here

我尝试使用 Team.id 内部加入 game.home 和 game.away,但收到错误:“错误:Class Travel\Bundle\TravelBundle\Entity\Game 没有名为 home 的关联“我做错了什么?

这是我想要达到的预期结果,我知道如何在 MySQL 中做到这一点,但不知道 Symfony 和 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;

这是我的 Doctrine 实体:

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

?>

最佳答案

根据doctrine documentation您的映射应如下所示:

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

关于php - Symfony2 + Doctrine 关联错误 : Class Game has no association named home,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25476732/

相关文章:

mysql - CakePHP 3 - MySQL 'BIGINT' 字段在实体中未正确处理

php - PDO 不绑定(bind)变量

symfony - 一对多关系不起作用

templates - Sonata管理员 bundle -表单类型: sonata_type_collection - custom template?

mysql - 创建像 IF MySQL 函数这样的自定义 DQL 但它不起作用 - [Symfony 2]

php - 在网站中使用 reCAPTCHA PHP 插件的最佳简单方法

PHP mysqli_fetch_assoc() 函数在同一个 PHP 文件工作时返回 NULL

带有 "Group by"和 "max"和 "join"的 SQL 请求?

php - Doctrine可为空的一对一关系仍然要创建唯一索引

php - 查询未在 PHP 中给出预期结果