2015-11-16 1 views
0

Je suivais le didacticiel symfony2 sur la création d'une collection de formulaires incorporés, mais je n'ai pas pu l'implémenter car elle crée uniquement une table de jonction. Selon la documentation de doctrine2: "Pourquoi les associations plusieurs-à-plusieurs sont-elles moins fréquentes? Parce que souvent vous souhaitez associer des attributs supplémentaires à une association, auquel cas vous introduisez une classe d'association. - beaucoup d'associations disparaissent et sont remplacées par des associations un-à-plusieurs/plusieurs-à-un entre les trois classes participantes. "Collection de formulaires intégrés et mappage d'entités symfony2

Voici quelques extraits de mon code:

src/AppBundle/Entité/ingredient.php

/** 
* Defines the properties of the Ingredient entity to represent the portal ingredients. 
* 
* @author furious_snail 
* 
* @ORM\Entity() 
* @ORM\Table(name="ingredients") 
* @UniqueEntity("name") 
*/ 
class Ingredient 
{ 
/** 
* @ORM\Id() 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 
/** 
* @ORM\ManyToOne(targetEntity="IngredientCategory") 
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true) 
*/ 
private $category; 
/** 
* @ORM\Column(type="string", unique=true) 
*/ 
private $name; 
/** 
* @ORM\OneToMany(targetEntity="IngredientNutrient", mappedBy="ingredient", cascade={"persist", "remove"}) 
*/ 
private $nutrientsPer100G; 

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

public function getId() 
{ 
    return $this->id; 
} 

public function setName($name) 
{ 
    $this->name = $name; 
} 

public function getName() 
{ 
    return $this->name; 
} 

/** 
* @param mixed $nutrientsPer100G 
*/ 
public function setNutrientsPer100G($nutrientsPer100G) 
{ 
    $this->nutrientsPer100G = $nutrientsPer100G; 
} 

/** 
* @return array 
*/ 
public function getNutrientsPer100G() 
{ 
    return $this->nutrientsPer100G; 
} 
} 

src/AppBundle/Entité/IngredientNutrient.php

/** 
* @ORM\Entity() 
* @ORM\Table(name="ingredient_nutrient") 
*/ 
class IngredientNutrient 
{ 
/** 
* @ORM\Id() 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 
/** 
* @var integer 
* 
* @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="nutrientsPer100G") 
* @ORM\JoinColumn(name="ingredient_id", referencedColumnName="id", nullable=true) 
*/ 
protected $ingredient; 
/** 
* @var integer 
* 
* @ORM\ManyToOne(targetEntity="Nutrient") 
* @ORM\JoinColumn(name="nutrient_id", referencedColumnName="id", nullable=true) 
*/ 
protected $nutrient; 
/** 
* @ORM\Column(type="float") 
*/ 
private $quantity; 
/** 
* @ORM\ManyToOne(targetEntity="Unit") 
* @ORM\JoinColumn(name="unit_id", referencedColumnName="id", nullable=true) 
*/ 
private $unit; 

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

/** 
* @return mixed 
*/ 
public function getIngredient() 
{ 
    return $this->ingredient; 
} 

/** 
* @param mixed $ingredient 
*/ 
public function setIngredient($ingredient) 
{ 
    $this->ingredient = $ingredient; 
} 

/** 
* @return mixed 
*/ 
public function getNutrient() 
{ 
    return $this->nutrient; 
} 

/** 
* @param mixed $nutrient 
*/ 
public function setNutrient($nutrient) 
{ 
    $this->nutrient = $nutrient; 
} 

/** 
* @return mixed 
*/ 
public function getQuantity() 
{ 
    return $this->quantity; 
} 

/** 
* @param mixed $quantity 
*/ 
public function setQuantity($quantity) 
{ 
    $this->quantity = $quantity; 
} 

/** 
* @return mixed 
*/ 
public function getUnit() 
{ 
    return $this->unit; 
} 

/** 
* @param mixed $unit 
*/ 
public function setUnit($unit) 
{ 
    $this->unit = $unit; 
} 
} 

src/AppBundle/Form/Type/IngredientNutrientType.php

class IngredientNutrientType extends AbstractType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('nutrient', 'entity', array(
      'class' => 'AppBundle\Entity\Nutrient', 
      'choice_label' => 'name', 
      'label' => 'Nutrient', 
     )) 
     ->add('quantity', null, array('label' => 'Cantitate')) 
     ->add('unit', 'entity', array(
      'class' => 'AppBundle\Entity\Unit', 
      'choice_label' => 'unit', 
      'label' => 'Unitate de masura' 
     )); 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\IngredientNutrient', 
    )); 
} 

public function getName() 
{ 
    return 'app_ingredient_nutrient'; 
} 
} 

src/AppBundle/Forme/Type/IngredientType.php

class IngredientType extends AbstractType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('name', null, array('label' => 'Name')) 
     ->add('nutrients_per_100_g', 'collection', array(
      'type' => new IngredientNutrientType(), 
      'allow_add' => true, 
      'label' => 'Nutrient quantity per 100g', 
      'options' => array('label' => false), 
     )); 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Ingredient', 
    )); 
} 

public function getName() 
{ 
    return 'app_ingredient'; 
} 
} 

Cela fonctionne, je reçois une collection de forme intégrée, mais le problème est que la ingredient_id dans la table ingredient_nutrient est nulle. Comment puis-je faire pour remplir la table avec le bon ID?

Ce sont les champs que je reçois sur la page:

Nom:

éléments nutritifs:

Quantité:

Unité:

L'idée est que si je IngredientNutrient forme liée avec la forme d'ingrédient l'utilisateur ne devrait pas avoir à spécifier le nom d'ingrédient deux fois.

Merci.

Répondre

0

Pour commencer, vous devez « référence croisée » vos entités:

public function setNutrientsPer100G($nutrientsPer100G) 
{ 
    $this->nutrientsPer100G = $nutrientsPer100G; 
    $nutrientsPer100G->setIngrediant($this); 
} 

Assurez-vous sont fixés les deux côtés de la relation. Cela prendra soin des problèmes d'identification nulle.

L'autre problème est que vous utilisez des collections dans vos entités Ingrédient/Nutriment, mais que vos méthodes set n'utilisent pas les opérateurs de tableau.