学说在JOIN SELECT上返回null对象



我在我的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();
}

查询将最后一个记录类别(以别名c)为null值,如果我通过"选择S"更改选择行,我将通过学说lazyloading获得正确的结果,我想避免懒惰。

例如,如果我在存储库查询中有四个名为" C1,C2,C3,C4"的类别,我将获得C4为null。

我的班级看起来像这样(请注意,我正在使用多对一的单向关系来避免双向关系)

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * ShopCategory
 *
 * @ORMTable()
 * @ORMEntity
  */
class ShopCategory
{

    /**
     * @ORMId
     * @ORMManyToOne(targetEntity="AppBundleEntityShop")
     */
    private $Shop;

    /**
     * @ORMId
     * @ORMManyToOne(targetEntity="AppBundleEntityCategory")
     */
    private $Category;

    /**
     * Set Shop
     *
     * @param AppBundleEntityShop $Shop
     * @return ShopCategory
     */
    public function setShop(AppBundleEntityShop $Shop)
    {
    $this->Shop = $Shop;
    return $this;
    }
    /**
     * Get Shop
     *
     * @return AppBundleEntityShop
     */
    public function getShop()
    {
    return $this->Shop;
    }
    /**
     * Set Category
     *
     * @param AppBundleEntityCategory $Category
     * @return ShopCategory
     */
    public function setCategory(AppBundleEntityCategory $Category)
    {
    $this->Category = $Category;
    return $this;
    }
    /**
     * Get Category
     *
     * @return AppBundleEntityCategory 
     */
    public function getCategory()
    {
    return $this->Category;
    }
}

尝试此

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

相关内容

  • 没有找到相关文章

最新更新