实体类没有像我的DB中那样命名的字段或关联


Entity class has no field or association named like in my DB

我想创建一个根据e.rules_id、e.status_id 恢复数字的请求

public function findStat() 
{
 $types = $this->em
        ->getRepository('EthanRestBundle:EthanRules')
        ->createQueryBuilder('e')
        ->select('e.rules_id, e.status_id, COUNT(*)')
        ->groupBy('e.rules_id, e.status_id')
        ->orderBy('rules_id');
    $types = $types->getQuery()->getSingleScalarResult();
    return $types;
}

错误消息:

[语义错误]第0行,第9列,靠近'rules_id,e.status_id,':错误:类Ethan''RestBundle''Entity''EthanRules没有名为rules_id"

我使用FosrestBundle在Symfony 2上进行开发,以创建REST应用程序

namespace Ethan'RestBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * EthanRules
 *
 * @ORM'Table(name="ethan_rules")
 * @ORM'Entity
 */
class EthanRules
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer", nullable=false)
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM'Column(name="customer_key", type="string", length=45, nullable=false)
     */
    private $customerKey;
    /**
     * @var string
     *
     * @ORM'Column(name="subscription_key", type="string", length=45, nullable=true)
     */
    private $subscriptionKey;
    /**
     * @var string
     *
     * @ORM'Column(name="pole", type="string", length=45, nullable=false)
     */
    private $pole;
    /**
     * @var string
     *
     * @ORM'Column(name="link_number", type="string", length=255, nullable=false)
     */
    private $linkNumber;
    /**
     * @var string
     *
     * @ORM'Column(name="user", type="string", length=100, nullable=true)
     */
    private $user;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="date_rdv", type="datetime", nullable=true)
     */
    private $dateRdv;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="created_at", type="datetime", nullable=false)
     */
    private $createdAt;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="updated_at", type="datetime", nullable=false)
     */
    private $updatedAt;
    /**
     * @var 'Rules
     *
     * @ORM'ManyToOne(targetEntity="Rules")
     * @ORM'JoinColumns({
     *   @ORM'JoinColumn(name="rules_id", referencedColumnName="id")
     * })
     */
    private $rules;
    /**
     * @var 'Status
     *
     * @ORM'ManyToOne(targetEntity="Status")
     * @ORM'JoinColumns({
     *   @ORM'JoinColumn(name="status_id", referencedColumnName="id")
     * })
     */
    private $status;

    public function _construct()
    {
        $this->createdAt = new 'DateTime();
        $this->createdAt->setTimestamp(time());
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set customerKey
     *
     * @param string $customerKey
     * @return EthanRules
     */
    public function setCustomerKey($customerKey)
    {
        $this->customerKey = $customerKey;
        return $this;
    }
    /**
     * Get customerKey
     *
     * @return string 
     */
    public function getCustomerKey()
    {
        return $this->customerKey;
    }
    /**
     * Set subscriptionKey
     *
     * @param string $subscriptionKey
     * @return EthanRules
     */
    public function setSubscriptionKey($subscriptionKey)
    {
        $this->subscriptionKey = $subscriptionKey;
        return $this;
    }
    /**
     * Get subscriptionKey
     *
     * @return string 
     */
    public function getSubscriptionKey()
    {
        return $this->subscriptionKey;
    }
    /**
     * Set pole
     *
     * @param string $pole
     * @return EthanRules
     */
    public function setPole($pole)
    {
        $this->pole = $pole;
        return $this;
    }
    /**
     * Get pole
     *
     * @return string 
     */
    public function getPole()
    {
        return $this->pole;
    }
    /**
     * Set linkNumber
     *
     * @param string $linkNumber
     * @return EthanRules
     */
    public function setLinkNumber($linkNumber)
    {
        $this->linkNumber = $linkNumber;
        return $this;
    }
    /**
     * Get linkNumber
     *
     * @return string 
     */
    public function getLinkNumber()
    {
        return $this->linkNumber;
    }
    /**
     * Set user
     *
     * @param string $user
     * @return EthanRules
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return string 
     */
    public function getUser()
    {
        return $this->user;
    }
    /**
     * Set dateRdv
     *
     * @param 'DateTime $dateRdv
     * @return EthanRules
     */
    public function setDateRdv($dateRdv)
    {
        $this->dateRdv = $dateRdv;
        return $this;
    }
    /**
     * Get dateRdv
     *
     * @return 'DateTime 
     */
    public function getDateRdv()
    {
        return $this->dateRdv;
    }
    /**
     * Set createdAt
     *
     * @param 'DateTime $createdAt
     * @return EthanRules
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
        return $this;
    }
    /**
     * Get createdAt
     *
     * @return 'DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
    /**
     * Set updatedAt
     *
     * @param 'DateTime $updatedAt
     * @return EthanRules
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
        return $this;
    }
    /**
     * Get updatedAt
     *
     * @return 'DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
    /**
     * Set rules
     *
     * @param 'Ethan'RestBundle'Entity'Rules $rules
     * @return EthanRules
     */
    public function setRules('Ethan'RestBundle'Entity'Rules $rules = null)
    {
        $this->rules = $rules;
        return $this;
    }
    /**
     * Get rules
     *
     * @return 'Ethan'RestBundle'Entity'Rules 
     */
    public function getRules()
    {
        return $this->rules;
    }
    /**
     * Set status
     *
     * @param 'Ethan'RestBundle'Entity'Status $status
     * @return EthanRules
     */
    public function setStatus('Ethan'RestBundle'Entity'Status $status = null)
    {
        $this->status = $status;
        return $this;
    }
    /**
     * Get status
     *
     * @return 'Ethan'RestBundle'Entity'Status 
     */
    public function getStatus()
    {
        return $this->status;
    }
}

由于您尚未定义属性$rules_id原则无法选择属性。相反,加入两个实体并选择它们的id:

$types = $this->em
    ->getRepository('EthanRestBundle:EthanRules')
    ->createQueryBuilder('e')
    ->select('r.id AS rule_id, s.id AS status_id, COUNT(DISTINCT e.id)')
    ->leftJoin('e.rules', 'r')
    ->leftJoin('e.status', 's')
    ->groupBy('r.id, s.id')
    ->orderBy('r.id');

但是:您使用的getSingleScalarResult()无法处理多个结果行!getArrayResult()无疑是您的最佳选择,因为看起来您不想使用对象。

默认情况下,Doctrine在实体类中使用camelBase但在数据库中使用_underscores命名所有字段。

您需要相应地将rules_idstatus_id更改为rulesstatus