多对多的简单查询

时间:2014-05-30 12:16:45

标签: sql symfony doctrine-orm doctrine doctrine-query

无法为DB创建简单查询。

我的实体Givetask:

<?php

namespace RoSky\Bundle\GwsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * GivenTask
 */
class GivenTask
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $squad;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $task;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->squad = new \Doctrine\Common\Collections\ArrayCollection();
        $this->task = new \Doctrine\Common\Collections\ArrayCollection();
    }

...GETTER SETTERS

我的实体小队:

<?php

namespace RoSky\Bundle\GwsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Squad
 */
class Squad
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

.....GETTERS SETTERS

我的.yaml配置GivenTask实体:

RoSky\Bundle\GwsBundle\Entity\GivenTask:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
    manyToMany:
        squad:
          targetEntity: Squad
          joinTable:
            name: SquadToGivenTask
            joinColumns:
              given_task_id:
                referencedColumnName: id
                nullable: false
            inverseJoinColumns:
              squad_id:
                referencedColumnName: id
                nullable: false
        task:
          targetEntity: Task
          joinTable:
            name: TaskToGivenTask
            joinColumns:
              given_task_id:
                referencedColumnName: id
                nullable: false
            inverseJoinColumns:
              task_id:
                referencedColumnName: id
                nullable: false
    lifecycleCallbacks: {  }

我的.yaml配置Squad实体:

RoSky\Bundle\GwsBundle\Entity\Squad:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
        name:
          type: string
          length: 100
          nullable: false
    lifecycleCallbacks: { }

现在我尝试进行查询...

$test = $this->em->getRepository('RoSkyGwsBundle:GivenTask')->findBySquad(4);

而且......我得到了学说例外:

  

ContextErrorException:注意:未定义的索引:joinColumns in   /home/DEA7H/Documents/Server/GraphWebSystem/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php   第1665行

这是什么? =)

详细说明:

Symfony: 2.4

学说: 2.2.3

ORDBMS: PostgreSQL 9.2

先谢谢。

1 个答案:

答案 0 :(得分:0)

您遇到的问题可能是基于误解您尝试进行的通话的方向性。

调用$this->em->getRepository('RoSkyGwsBundle:GivenTask')->findBySquad(4);将首先查找ID为4的Squad Entity,并尝试使用GivenTask表进行连接。这与选择匹配链接到Squad 4的标准的GivenTasks形成对比,后者可以通过SELECTTasks的SELECT上的WHERE子句实现,来自GivenTask的左连接 - &gt;小队,或者简单地通过$this->em->getRepository('RoSkyGwsBundle:GivenTask')->findAll();迭代匹配Squad 4的GivenTasks。

findBySquad命令的方向性是从Squad到GivenTask。但是,GivenTask是拥有(或主体)实体,并且您的实体关系是多个单向。这意味着Doctrine只能找到基于GivenTasks的Squads,而不是相反。

要解决此问题,您需要将联接更改为多个 bidrectional ,如本教程文档的此部分所示:Many-to-Many Bidirectional Mapping

基本上,您需要将Squad.orm.yml文件修改为以下内容:

RoSky\Bundle\GwsBundle\Entity\Squad:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
        name:
          type: string
          length: 100
          nullable: false
    manyToMany:
        givenTasks:
            targetEntity: GivenTask
            mappedBy: squad
    lifecycleCallbacks: { }
相关问题