Can i use query in form builder to get filtered collection in symfony form

前端 未结 5 779
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 16:42

IN the AcmePizza BUndle this is working fine

->add(\'pizza\', \'entity\', array(
                \'class\'         => \'Acme\\PizzaBundle\\Entity\\Pizza\',         


        
5条回答
  •  -上瘾入骨i
    2021-02-05 17:28

    Assuming Your userTasks is an relationship You will find answer for Your case here. These is just how to sort but if You had required some WHERE conditions it is not so simple but neither it is hard.

    I had to filter out some entities, the key to solve it was to create set/get method in entity class returning required set.

    In my case it looks like this

    /**
     * Set values
     *
     * @param ArrayCollection $values
     * @return Attribute
     */
    public function setCustomValues($values)
    {
        $result = $this->getNotCustomValues();
        foreach ($values as $value)
        {
            $value->setAttribute($this);
            $result->add($value);
        }
        $this->values = $result;
    
        return $this;
    }
    
    /**
     * Get values
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCustomValues()
    {
        $result = new ArrayCollection();
        foreach ($this->values as $value) {
            if($value->getCustom()) {
                $result->add($value);
            }
        }
        return $result;
    }
    

    And when creating form, name for a field is "customvalues" instead of "values" So my collection contains only values with custom field true.

提交回复
热议问题