Php Symfony2,具有属性的多对一关系的嵌入表单集合的呈现
我有两个实体,产品和功能示例:宽度、高度、组成。。。以及另一个中间实体ProductFeature,用于表示其他两个实体之间的关系 我希望在添加新产品时显示表单中的所有功能,如下所示: 当我们在PrestaShop中添加新产品时,它看起来有点像。我尝试使用该系列,但失败了 产品类型 产品特征类型 特征类型 form.html.twig 实体\特征 实体\产品特征Php Symfony2,具有属性的多对一关系的嵌入表单集合的呈现,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我有两个实体,产品和功能示例:宽度、高度、组成。。。以及另一个中间实体ProductFeature,用于表示其他两个实体之间的关系 我希望在添加新产品时显示表单中的所有功能,如下所示: 当我们在PrestaShop中添加新产品时,它看起来有点像。我尝试使用该系列,但失败了 产品类型 产品特征类型 特征类型 form.html.twig 实体\特征 实体\产品特征 请帮助您是否考虑过在产品和功能之间建立关系?您可以在之后使用symfony2。不,它必须是多对一,因为功能的每个值都与一个产品相关。
请帮助您是否考虑过在产品和功能之间建立关系?您可以在之后使用symfony2。不,它必须是多对一,因为功能的每个值都与一个产品相关。更好的是,为什么不将产品和功能实体相互关联?当Doctrine ORM处理这个问题时,为什么您需要一个中间实体?因为每个产品都可以有功能,每个功能都有不同的值,例如:我们有功能重量,product1的重量是1千克,product2的重量是3千克。所以它必须是另一个中间实体,将Id产品作为Id特征和值。将在名为productFeature的数据库中创建一个新表,该表的Id为product Id feature和Value。你明白了吗是的,我知道,但是如果在DB中需要一个中间表来处理这两个实体之间的关系,那么就有很多。你读过关于这种关系的条令文件吗?
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//.....
->add('productFeatures', 'collection', array(
'type' => new ProductFeatureType(),
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\StoreBundle\Entity\Product'
));
}
}
class ProductFeatureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value', 'text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\StoreBundle\Entity\ProductFeature'
));
}
}
class FeatureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'required' => true,
'label' => 'Feature',
'attr' => array('class' =>'form-control'),
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\StoreBundle\Entity\Feature'
));
}
}
{% for feature in form.productFeatures %}
<div class="form-group">
{{ form_label(form.feature, null, { 'label_attr': {'class': 'col-sm-3 control-label'} }) }}
<div class="col-sm-7">
{{ form_row(feature.name) }}
</div>
{{ form_errors(form.feature) }}
</div>
{% endfor %}
class Product
{
/**
* @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, nullable=false)
*/
private $name;
//....
/**
* @ORM\OneToMany(targetEntity="Project\StoreBundle\Entity\ProductFeature", mappedBy="product", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $productFeatures ;
public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
$this->productFeatures = new \Doctrine\Common\Collections\ArrayCollection();
}
//........
/**
* Add productFeatures
*
* @param \Project\StoreBundle\Entity\ProductFeature $productFeatures
* @return Product
*/
public function addProductFeature(\Project\StoreBundle\Entity\ProductFeature $productFeatures)
{
$this->productFeatures[] = $productFeatures;
return $this;
}
/**
* Remove productFeatures
*
* @param \Project\StoreBundle\Entity\ProductFeature $productFeatures
*/
public function removeProductFeature(\Project\StoreBundle\Entity\ProductFeature $productFeatures)
{
$this->productFeatures->removeElement($productFeatures);
}
/**
* Get productFeatures
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProductFeatures()
{
return $this->productFeatures;
}
class Feature
{
/**
* @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\ManyToOne(targetEntity="Project\StoreBundle\Entity\Store", inversedBy="features", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $store ;
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Feature
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set store
*
* @param \Project\StoreBundle\Entity\Store $store
* @return Feature
*/
public function setStore(\Project\StoreBundle\Entity\Store $store = null)
{
$this->store = $store;
return $this;
}
/**
* Get store
*
* @return \Project\StoreBundle\Entity\Store
*/
public function getStore()
{
return $this->store;
}
}
class ProductFeature
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Product", inversedBy="productFeatures")
*/
private $product;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Feature")
*/
private $feature;
/**
* @var string
*
* @ORM\Column(name="value", type="string", length=255)
*/
private $value;
/**
* Set value
*
* @param string $value
* @return ProductFeature
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set product
*
* @param \Project\StoreBundle\Entity\Product $product
* @return ProductFeature
*/
public function setProduct(\Project\StoreBundle\Entity\Product $product)
{
$product->addProductFeature($this);
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \Project\StoreBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set feature
*
* @param \Project\StoreBundle\Entity\Feature $feature
* @return ProductFeature
*/
public function setFeature(\Project\StoreBundle\Entity\Feature $feature)
{
$this->feature = $feature;
return $this;
}
/**
* Get feature
*
* @return \Project\StoreBundle\Entity\Feature
*/
public function getFeature()
{
return $this->feature;
}
}