当我想使用集合
处理表单时,我遇到了问题我有两个实体FeatureCategory和Feature
用户在每个featureCategory(featureCategories的集合)中选择一个功能。
要素类别实体:
class FeatureCategory
{
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\OneToMany(targetEntity="Site\BackendBundle\Entity\Feature", mappedBy="featureCategory", cascade={"persist"})
*/
private $features;
/**
* Constructor
*/
public function __construct()
{
$this->features = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add feature
*
* @param \Site\BackendBundle\Entity\Feature $feature
* @return FeatureCategory
*/
public function addFeature(\Site\BackendBundle\Entity\Feature $feature)
{
$feature->setFeatureCategory($this);
$this->features[] = $feature;
return $this;
}
/**
* Remove feature
*
* @param \Site\BackendBundle\Entity\Feature $feature
*/
public function removeFeature(\Site\BackendBundle\Entity\Feature $feature)
{
$this->features->removeElement($feature);
}
/**
* Get features
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getFeatures()
{
return $this->features;
}
}
特征实体:
class Feature
{
/**
* @var \Site\BackendBundle\Entity\FeatureCategory
*
* @ORM\ManyToOne(targetEntity="Site\BackendBundle\Entity\FeatureCategory", inversedBy="features")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="feature_category_id", referencedColumnName="id")
* })
*/
private $featureCategory;
/**
* Set featureCategory
*
* @param \Site\BackendBundle\Entity\FeatureCategory $featureCategory
* @return Feature
*/
public function setFeatureCategory(\Site\BackendBundle\Entity\FeatureCategory $featureCategory = null)
{
$this->featureCategory = $featureCategory;
return $this;
}
/**
* Get featureCategory
*
* @return \Site\BackendBundle\Entity\FeatureCategory
*/
public function getFeatureCategory()
{
return $this->featureCategory;
}
}
要素类别表单:
class FeatureCategoriesFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('featureCategories', 'collection', array(
'type' => 'site_frontend_feature_category',
'by_reference' => false
))
->add('quantity', 'text', array(
'data' => 1
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => array('feature_categories')
));
}
public function getName()
{
return 'site_frontend_feature_categories';
}
}
功能类别表单:
class FeatureCategoryFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder)
{
$form = $event->getForm();
$data = $event->getData();
/* Check we're looking at the right data/form */
if ($data instanceof FeatureCategory)
{
$choices = $data->getFeatures();
$form
->add('features', 'entity', array(
'multiple' => false,
'expanded' => true,
'class' => 'SiteBackendBundle:Feature',
'property' => 'value',
'choices' => $choices,
'data' => null
))
->add('name', 'hidden', array(
'read_only' => true,
'label' => $data->getName()
))
;
}
});
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Site\BackendBundle\Entity\FeatureCategory',
'validation_groups' => array('feature_category')
));
}
public function getName()
{
return 'site_frontend_feature_category';
}
}
最后我的控制器:
class OrderController extends Controller
{
public function addProductAction(Request $request, $slug)
{
$em = $this->get('doctrine.orm.default_entity_manager');
$item = $em->getRepository('SiteBackendBundle:Product')
->getItem($slug);
if (!$item) {
throw new NotFoundHttpException();
}
$featureCategories = $em->getRepository('SiteBackendBundle:FeatureCategory')
->getListByProduct($item);
$featureCategoriesForm = $this->createForm('site_frontend_feature_categories', array('featureCategories' => $featureCategories),
array(
'method' => 'POST',
'action' => $this->generateUrl('user_order_add_product', array(
'slug' => $item->getSlug(),
))
)
);
if ($request->isXmlHttpRequest()) {
if ($request->isMethod('POST')) {
$featureCategoriesForm->handleRequest($request);
if ($featureCategoriesForm->isValid()) {
//order add product logic
}
}
}
}
}
在$featureCategoriesForm->handleRequest($request);
行之后,我收到了错误
属性"功能"也不是其中一种方法 " addFeatur()" /" removeFeatur()"," addFeature()" /" removeFeature()", " setFeatures()"," features()"," __ set()"或" __ call()"存在并拥有 类中的公共访问" Site \ BackendBundle \ Entity \ FeatureCategory"。
很抱歉这篇长篇文章 有人可以帮忙吗?
答案 0 :(得分:0)
最后,我修复了使用实体作为具有choice
类型形式的数组的问题,没有data_class
等等