教义 - 执行 select 与关系 1:N

Doctrine - Perform select with relationship 1:N

我有许多实体(使用 Symfony2 控制台自动生成),它们在数据库中的结构如下:

生成的实体如下:

class Offers
{
    /**
     * @var string
     * @Assert\Length(
     *      min = 5,
     *      max = 64,
     *      minMessage = "El nombre de la oferta debe de tener al menos {{ limit }} caracteres.",
     *      maxMessage = "El nombre de la oferta no puede superar los {{ limit }} caracteres."
     * )
     */
    private $name;

    /**
     * @var string
     */
    private $description;

    /**
     * @var string
     * @Assert\Url(message = "La url '{{ value }}' no es válida")
     */
    private $url;

    /**
     * @var string
     */
    private $img;

    /**
     * @var \DateTime
     */
    private $dateFrom;

    /**
     * @var \DateTime
     */
    private $dateTo;

    /**
     * @var \DateTime
     */
    private $registered;

    /**
     * @var boolean
     */
    private $active;

    /**
     * @var integer
     */
    private $availableFor;

    /**
     * @var integer
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $idState;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->idState = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Offers
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Offers
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return Offers
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set img
     *
     * @param string $img
     */
    public function setImg($img = null)
    {
        if ( $img != null)
            $this->img = $img;
    }

    /**
     * Get img
     *
     * @return string 
     */
    public function getImg()
    {
        return $this->img;
    }

    /**
     * Set dateFrom
     *
     * @param \DateTime $dateFrom
     * @return Offers
     */
    public function setDateFrom($dateFrom)
    {
        $this->dateFrom = $dateFrom;

        return $this;
    }

    /**
     * Get dateFrom
     *
     * @return \DateTime 
     */
    public function getDateFrom()
    {
        return $this->dateFrom;
    }

    /**
     * Set dateTo
     *
     * @param \DateTime $dateTo
     * @return Offers
     */
    public function setDateTo($dateTo)
    {
        $this->dateTo = $dateTo;

        return $this;
    }

    /**
     * Get dateTo
     *
     * @return \DateTime 
     */
    public function getDateTo()
    {
        return $this->dateTo;
    }

    /**
     * Set registered
     *
     * @param \DateTime $registered
     * @return Offers
     */
    public function setRegistered($registered)
    {
        $this->registered = $registered;

        return $this;
    }

    /**
     * Get registered
     *
     * @return \DateTime 
     */
    public function getRegistered()
    {
        return $this->registered;
    }

    /**
     * Set active
     *
     * @param boolean $active
     * @return Offers
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set availableFor
     *
     * @param integer $availableFor
     * @return Offers
     */
    public function setAvailableFor($availableFor)
    {
        $this->availableFor = $availableFor;

        return $this;
    }

    /**
     * Get availableFor
     *
     * @return integer 
     */
    public function getAvailableFor()
    {
        return $this->availableFor;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add idState
     *
     * @param \Consolidador\PanelBundle\Entity\States $idState
     * @return Offers
     */
    public function addIdState(\Consolidador\PanelBundle\Entity\States $idState)
    {
        $this->idState[] = $idState;

        return $this;
    }

    /**
     * Remove idState
     *
     * @param \Consolidador\PanelBundle\Entity\States $idState
     */
    public function removeIdState(\Consolidador\PanelBundle\Entity\States $idState)
    {
        $this->idState->removeElement($idState);
    }

    /**
     * Get idState
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getIdState()
    {
        return $this->idState;
    }
    
    /**
     * Set idState
     * @param \Consolidador\PanelBundle\Entity\States $idState
     */
    public function setIdState($idState)
    {
        $this->idState[] = $idState;
    }
    
   /**
    * Cadena de texto a devolver.
    * @return string
    */
    public function __toString()
    {
        return $this->name;
    }

原来我select属于某一个州的招标,我的做法是这样的:

$dql = $em->createQuery("SELECT o FROM PanelBundle:Offers o WHERE o.idState = :state");
        $dql->setParameter('state', $this->getUser()->getIdAgency()->getIdZone()->getIdState());
        $offersState = $dql->getResult();

但是我返回了以下错误:

[Semantical Error] line 0, col 43 near 'idState = :s': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

有没有人想到什么?你知道在这样的机构怎么咨询吗?

问候和谢谢

已解决:

这里是用于特定状态的产品和不属于任何(独立)的所有产品的 DQL 查询。可能有更简单的方法,如果有人知道,你发布一下。

SELECT o FROM PanelBundle:Offers o JOIN o.idState os WHERE os.id = :state

SELECT o FROM PanelBundle:Offers o WHERE o.id NOT IN (SELECT x.id FROM PanelBundle:Offers x JOIN x.idState os)

您应该加入状态 table。

$qb
  ->from('PanelBundle:Offers', 'o')
  ->select('o')
  ->join('PanelBundle:States', 's')
  ->andWhere('o.state = :state');
  // ....