我在symfony类库中有这个功能:
public function findAllByIdShop($id)
{
return $this->getEntityManager()
->createQuery(
'SELECT s, c
FROM AppBundle:ShopCategory s
JOIN s.category c
WHERE s.shop = :shop_id
ORDER BY c.name'
)
->setParameter(':shop_id', $id)
->getResult();
}
如果我通过" SELECT s"更改选择行,则查询将最后一个记录类别(带别名c)返回为NULL值。我将使用doctrine lazyloading获得正确的结果,我想避免延迟加载。
例如,如果我有四个名为" c1,c2,c3,c4和#34;在存储库查询中,我将获得c4为null。
我的班级看起来像那样(注意我使用多对一,单向关系来避免双向关系)
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ShopCategory
*
* @ORM\Table()
* @ORM\Entity
*/
class ShopCategory
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Shop")
*/
private $Shop;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category")
*/
private $Category;
/**
* Set Shop
*
* @param AppBundle\Entity\Shop $Shop
* @return ShopCategory
*/
public function setShop(\AppBundle\Entity\Shop $Shop)
{
$this->Shop = $Shop;
return $this;
}
/**
* Get Shop
*
* @return AppBundle\Entity\Shop
*/
public function getShop()
{
return $this->Shop;
}
/**
* Set Category
*
* @param AppBundle\Entity\Category $Category
* @return ShopCategory
*/
public function setCategory(\AppBundle\Entity\Category $Category)
{
$this->Category = $Category;
return $this;
}
/**
* Get Category
*
* @return AppBundle\Entity\Category
*/
public function getCategory()
{
return $this->Category;
}
}
答案 0 :(得分:0)
试试这个
public function findAllByIdShop($id)
{
return $this->getEntityManager()
->createQuery(
'SELECT s, c
FROM AppBundle:ShopCategory sc
JOIN sc.category c
JOIN sc.shop s
WHERE s.<id_field> = :shop_id
ORDER BY c.name'
)
->setParameter(':shop_id', $id)
->getResult();
}