错误:如果不选择至少一个根实体别名,则无法通过标识变量选择实体

时间:2016-03-20 21:23:20

标签: php mysql symfony doctrine-orm

我在我的存储库中使用此查询构建器:

public function findByCityCategory($city, $category)
{
    $qb = $this->createQueryBuilder('e')
        ->select(['e.id','e.title','e.address', 'e.lat', 'e.lng', 'e.siteUrl', 'e.phoneNo', 'w', 'd.id as category', 'avg(r.rating) as rating'])
        ->innerJoin('e.workingTimes', 'w')
        ->innerJoin('e.category', 'd')
        ->where('d.id = :categoryId')
        ->andWhere('e.city = :cityId')
        ->leftJoin('e.ratings', 'r')
        ->groupBy('r.place')
        ->setParameter('categoryId', $category)
        ->setParameter('cityId', $city);

    return $qb->getQuery()->getResult();
}

但是当我尝试执行它时,我得到:   "message": "[Semantical Error] line 0, col -1 near 'SELECT e.id,': Error: Cannot select entity through identification variables without choosing at least one root entity alias.", "class": "Doctrine\\ORM\\Query\\QueryException",

我找了类似的问题herehere,但这些都不适合我。我的实体看起来像这样:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/** 
 * Place
 *
 * @ORM\Table(name="place")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PlaceRepository")
 */
class Place
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="address", type="string", length=255)
 */
private $address;

/**
 * @var float
 * @Assert\NotBlank()
 * @ORM\Column(name="lat", type="float")
 */
private $lat;

/**
 * @var float
 * @Assert\NotBlank()
 * @ORM\Column(name="lng", type="float")
 */
private $lng;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="text", nullable=true)
 */
private $description;

/**
 * @var string
 *
 * @ORM\Column(name="fb_page", type="string", length=255, nullable=true)
 */
private $fbPage;

/**
 * @Assert\NotBlank()
 * @ORM\ManyToOne(targetEntity="City")
 * @ORM\JoinColumn(name="city_id", referencedColumnName="id")
 */
private $city;

/**
 * @Assert\NotBlank()
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="places")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
private $category;

/**
 * @var string
 *
 * @ORM\Column(name="site_url", type="string", length=255, nullable=true)
 */
private $siteUrl;

/**
 * @ORM\ManyToMany(targetEntity="WorkingTime", cascade={"persist"})
 * @ORM\JoinTable(name="places_workingtimes",
 *      joinColumns={@ORM\JoinColumn(name="place_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="workingtime_id", referencedColumnName="id", unique=true)}
 *      )
 */
private $workingTimes;

/**
 * @var string
 * @ORM\Column(name="phone_no", type="string", length=255, nullable=true)
 *
 */
private $phoneNo;

/**
 * @ORM\OneToMany(targetEntity="Rating", mappedBy="place")
 */
private $ratings;

} `     

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

 /**
 * Rating
 *
 * @ORM\Table(name="rating")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RatingRepository")
 */
class Rating
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var int
 *
 * @ORM\Column(name="rating", type="smallint")
 */
private $rating;

/**
 * @ORM\ManyToOne(targetEntity="Place", inversedBy="ratings")
 * @ORM\JoinColumn(name="place_id", referencedColumnName="id")
 */
private $place;

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

当我尝试选择'w'时,才会出现此错误。那么,我该如何加入这个对象集合呢?

2 个答案:

答案 0 :(得分:2)

如果您只想从实体获取部分字段,则必须按照文档中的说明使用PARTIAL关键字:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/partial-objects.html

如果使用ORM查询构建器,最好加载所有字段而不是某些字段,因为ORM用于处理对象。如果只需要几个字段,则最好使用本机SQL查询构建器。

答案 1 :(得分:0)

我设法通过使用JMS Serializer的Exclude()注释从Place实体中排除不必要的字段而不是从实体中选择必需的字段来实现。