简体   繁体   English

symfony2嵌入式表单收集和实体映射

[英]symfony2 embedded form collection and entity mapping

I was following the symfony2 tutorial on how to create an embedded form collection but wasn't able to implement it since it only creates a junction table. 我一直遵循有关如何创建嵌入式表单集合的symfony2教程,但由于仅创建联结表而无法实现它。

According to doctrine2 documentation: "Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes." 根据doctrine2文档的说明:“为什么多对多关联不那么常见?因为经常要将其他属性与一个关联相关联,因此引入了关联类。因此,直接的多对多关联消失了,由3个参与班级之间的一对多/多对一关联取代。”

Here are some snippets of my code: 以下是我的代码片段:

src/AppBundle/Entity/Ingredient.php src / AppBundle / Entity / 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/Entity/IngredientNutrient.php src / AppBundle / Entity / 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 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/Form/Type/IngredientType.php src / AppBundle / Form / 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';
}
}

This works, I do get an embedded form collection but the issue is that the ingredient_id in the ingredient_nutrient table is null. 这项工作有效,但我确实获得了一个嵌入式表单集合,但问题是Ingredient_营养表中的Ingredient_id为空。 How do I make it to fill the table with the right ID? 如何使用正确的ID填写表格?

These are the fields I get on the page: 这些是我在页面上获得的字段:

Name: 名称:

Nutrient: 养分:

Quantity: 数量:

Unit: 单元:

The idea is that if I have IngredientNutrient form tied with Ingredient form the user shouldn't have to specify ingredient name twice. 这个想法是,如果我将IngredientNutrient形式与Ingredient形式联系在一起,则用户不必两次指定成分名称。

Thank you. 谢谢。

To start with, you need to "cross reference" your entities: 首先,您需要“交叉引用”您的实体:

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

Make sure both sides of the relation are being set. 确保关系的两侧都已设置。 That will take care of the null id issues. 这将解决null id问题。

The other problem is that you are using collections in your Ingrediant/Nutrient entities but your set methods are not using the array operators. 另一个问题是您在Ingrediant / Nutrient实体中使用集合,但是set方法未使用数组运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM