Symfony2 / Form - 选择关系数据

时间:2015-08-18 16:17:58

标签: symfony doctrine-orm relationship formbuilder

我有OneToMany关系 - > 1个类别有多个子类别,一切正常,但现在我需要将类别和子类别插入另一个表。

我想创建带有类别选择的表单

->add('category', 'entity', array(
                'class' => 'DbBundle\Entity\Category',
                'property' => 'name',
                'choice_value' => 'name',
                'placeholder' => 'Select category',
                'empty_data' => null,
                'required' => false
            ))

和子类别,但这是我的问题,如果我知道类别,我不知道如何在formbuilder子类别中生成。

示例:如果类别是汽车,则子类别字段必须显示宝马,奥迪......

类别实体:

class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255)
     */
    private $description;
    /**
     * @var string
     *
     * @ORM\Column(name="icon", type="string", length=255)
     */
    private $icon;
    /**
     * @ORM\Column(name="url", type="string", length=255)
     */
    private $url;

    /**
     *
     * @ORM\OneToMany(targetEntity="Subcategory", mappedBy="category")
     **/
    private $subcategories;

    public function __construct()
    {
        $this->subcategories = new ArrayCollection();
    }

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

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

        return $this;
    }

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

    /**
     * Add subcategories
     *
     * @param \DbBundle\Entity\Subcategory $subcategories
     * @return Category
     */
    public function addSubcategory(Subcategory $subcategories)
    {
        $this->subcategories[] = $subcategories;

        return $this;
    }

    /**
     * Remove subcategories
     *
     * @param \DbBundle\Entity\Subcategory $subcategories
     */
    public function removeSubcategory(Subcategory $subcategories)
    {
        $this->subcategories->removeElement($subcategories);
    }

    /**
     * Get subcategories
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSubcategories()
    {
        return $this->subcategories;
    }

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set icon
     *
     * @param string $icon
     * @return Category
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;

        return $this;
    }

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

子类别实体:

class Subcategory
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\Column(name="url", type="string", length=255)
     */
    private $url;

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

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

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

        return $this;
    }

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

    /**
     * Set category
     *
     * @param \DbBundle\Entity\Category $category
     * @return Subcategory
     */
    public function setCategory(Category $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \DbBundle\Entity\Category 
     */
    public function getCategory()
    {
        return $this->category;
    }

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

        return $this;
    }

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

我尝试使用

'query_builder' => function (CategoryRepository $repository)
                {
                    return $repository->createQueryBuilder('s')
                        ->select('s')
                        ->join('s.subcategories', 'c')
                        ->where('s.url = :url')
                        ->setParameter('url', $category);
                },

$ category是网址中的参数,但错误:$ category is Undefined

修改 这适用于(通过数据生成选择):

'class' => 'DbBundle\Entity\Subcategory',
                'query_builder' => function (SubcategoryRepository $repository)
                {
                    return $repository->createQueryBuilder('s')
                        ->select('s')
                        ->join('s.category', 'c')
                        ->where('s.category = :url')
                        ->setParameter('url', 1);
                },
                'property' => 'name',
                'choice_value' => 'url',

但是,如果我点击提交,我有错误:此值无效。我该如何解决?

0 个答案:

没有答案