我有OneToMany关系(类别和子类别),一切正常,但现在我尝试在知道类别ID时从子类别表中生成选择,并将数据设置为另一个表。
我有FormType,在其中:
$category = $options['categoryId'];
...
->add('subcategory', 'entity', array(
'class' => 'DbBundle\Entity\Subcategory',
'query_builder' => function (SubcategoryRepository $repository) use($category)
{
return $repository->getSubcategoriesByCategoryId($category);
},
'property' => 'name',
'placeholder' => 'Select category',
'empty_data' => null,
'required' => false
))
...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'categoryId' => null,
'csrf_protection' => true,
'csrf_field_name' => '_token',
'intention' => 'task_item',
));
}
SubcategoryRepository:
public function getSubcategoriesByCategoryId($categoryId)
{
$qb = $this->createQueryBuilder('s')
->select('s')
->join('s.category', 'c')
->where('s.category = :url')
->setParameter('url', $categoryId);
return $qb;
}
类别实体:
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;
public function __toString()
{
return (string) $this->getCategory();
}
/**
* 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;
}
}
如果我加载页面,我看到带有数据的选择字段,看起来一切都很好,但是如果我提交表单,我有错误:此值无效
我该如何解决?
由于
答案 0 :(得分:0)
删除此规则:
'property' => 'name',
'choice_value' => 'name',