1 pièce(s) jointe(s)
Imbriquer une Collection de Formulaires , Relation Many-To-One avec attributs
Salut
J'ai deux entités, Product et Feature (caractéristique: largeur, hauteur, composition ...) et une autre entité intermédiaire ProductFeature qui fait la relation entre les deux autres entités.
Je voudrais afficher toutes les Features dans la forme lors de l'ajout de nouveaux produits comme celui-ci mais j'ai pas réussi :
Pièce jointe 153221
Voici mon code :
ProductType
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
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'
));
}
} |
ProductFeatureType
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
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'
));
}
} |
FeatureType
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
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'
));
}
} |
form.html.twig
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
{% 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 %} |
Entity\Product
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
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;
} |
Entity\Feature
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
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;
}
} |
Entity\ProductFeature
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
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;
}
} |
Merci d'avance