duanla1996 2012-03-05 17:26
浏览 55
已采纳

Symfony和Doctrine使连接的查询起作用

I've got an SQL query that returns all the rows in one table (country) which have a related entry in another table (ducks) but I'm struggling to turn this into DQL. This is a standard one-many relationship as each country can have multiple ducks, I believe it is all set up correctly as I can return ducks within a country and return the country a duck is in using standard code.

The working query is:

SELECT c.* FROM country c 
INNER JOIN ducks d 
ON c.id = d.country_id 
GROUP BY c.country 
ORDER BY c.country ASC

I've tried converting this to:

SELECT c FROM WfukDuckBundle:Country c 
INNER JOIN WfukDuckBundle:Ducks d 
ON c.id = d.country_id 
GROUP BY c.country 
ORDER BY c.country ASC

which produces the following error:

[Semantical Error] line 0, col 79 near 'd ON': Error: Identification Variable 
WfukDuckBundle:Ducks used in join path expression but was not defined before. 

I'm quite new to Symfony/Doctrine so I suspect it's probably something obvious!

I'm using Symfony 2.0.11 with doctrine

Update: I've Also tried:

SELECT c FROM WfukDuckBundle:Country c 
INNER JOIN c.ducks d 
ON c.id = d.country_id 
GROUP BY c.country 
ORDER BY c.country ASC

where 'ducks' is defined in the Country class as:

/**
 * @ORM\OneToMany(targetEntity="Ducks", mappedBy="country")
 */
protected $ducks;

public function __construct()
{
    $this->ducks = new ArrayCollection();
}

the definition for country in the ducks class is:

/**
* @ORM\ManyToOne(targetEntity="Country", inversedBy="ducks")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id")
*/
private $country;
  • 写回答

1条回答 默认 最新

  • douba7784 2012-03-05 20:34
    关注

    Do yourself a favour and use the query builder. Easier to read and update and reuse your queries

    <?php
    
    namespace Vendor\Prefix\Repository;
    
    use Doctrine\ORM\EntityRepository;
    
    class SomeRepository extends EntityRepository
    {
        public function countryDucks()
        {
            // $em is the entity manager
            $qb = $em->createQueryBuilder();
    
            $qb
                ->select('country', 'duck')
                ->from('WfukDuckBundle:Country', 'country')
                ->innerJoin('country.ducks', 'duck')
                ->groupBy('country.country')
                ->orderBy('country.country', 'ASC')
            ;
    
            $query = $qb->getQuery();
    
            // Potential Hydration Modes
            // --------------------------------
            // Doctrine\ORM\Query::HYDRATE_OBJECT
            // Will give you an array of your object entities
            // --------------------------------
            // Doctrine\ORM\Query::HYDRATE_ARRAY 
            // Will give you an array mimicking 
            // your object graph 
            // --------------------------------
    
            return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀