【问题标题】:Entity class has no field or association named like in my DB实体类没有像我的数据库中那样命名的字段或关联
【发布时间】:2014-10-17 14:29:06
【问题描述】:

我想创建一个根据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;

}

错误信息:

[语义错误] line 0, col 9 near 'rules_id, e.status_id,': Error: Class Ethan\RestBundle\Entity\EthanRules has no field or association named rules_id"

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

【问题讨论】:

  • 显示您的 EnthanRules 实体字段名称,可能您在实体和数据库中有不同的名称。例如,如果在实体中你有 public $rulesId 你改变你的代码像 e.rulesId 而不是 e.rules_id
  • 问题不在于计数(*),而在于错误。请更新您的标题

标签: php symfony doctrine-orm


【解决方案1】:
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;
    }
}

【讨论】:

    【解决方案2】:

    由于您尚未定义属性$rules_id Doctrine 无法选择一个。而是加入两个实体并选择它们的 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() 将是您的最佳选择,因为您似乎不想使用对象。

    【讨论】:

    • @Ali 这对你有用吗?如果不告诉我有什么问题。否则你可以接受这个答案是正确的
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多