简体   繁体   English

原则在联接选择上返回空对象

[英]Doctrine returns null object on JOIN select

I have this function on my symfony class repository: 我的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();
}

The query returns the last record category (with alias c) as NULL value, if I change the select line by "SELECT s" I will get the correct results with doctrine lazyloading and I would like to avoid lazyloading. 该查询将最后一个记录类别(别名为c)作为NULL值返回,如果我通过“ SELECT s”更改选择行,则在使用lazyloading时将获得正确的结果,并且我希望避免lazyloading。

For example if I have four categories named "c1, c2, c3, c4" in the repository query, i'll obtain c4 as null. 例如,如果我在存储库查询中有四个名为“ c1,c2,c3,c4”的类别,我将获得c4为空。

My class look like that (note that i'm using Many-To-One, Unidirectional relations to avoid bidirectional relations) 我的课看起来像这样(请注意,我正在使用多对一,单向关系来避免双向关系)

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

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM