symfony easyadmin 一对多形式

标签 symfony one-to-many symfony2-easyadmin

我是 Easyadmin bundle 的新手,我正在寻找是否可以直接从父对象添加子对象 所以我得到了 3 个对象: - 食谱

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Recipe
 *
 * @ORM\Table(name="recipe")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RecipeRepository")
 */
class Recipe
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=true)
     */
    private $name;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdon", type="datetime", nullable=true)
     */
    private $createdon;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="version", type="string", length=5, nullable=true)
     */
    private $version;

    /**
     * @ORM\OneToMany(targetEntity="Recipe_Product", mappedBy="recipe")
    */
    private $recipeproducts;
...

-Recipe_Product(具有数量和单位作为要输入的属性)

   namespace AppBundle\Entity;

   use Doctrine\ORM\Mapping as ORM;

   /**
     * Recipe_Product
     *
     * @ORM\Table(name="recipe__product")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\Recipe_ProductRepository")
     */
class Recipe_Product
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="quantity", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $quantity;


    /**
     * @ORM\ManyToOne(targetEntity="Recipe", inversedBy="recipeproducts")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="recipeid", referencedColumnName="id")
     * })
     */
    private $recipe;


    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="recipeproducts")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Productid", referencedColumnName="id")
     * })
     */
    private $product;

    /**
     * @ORM\ManyToOne(targetEntity="Unit", inversedBy="recipeproducts")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Unitid", referencedColumnName="id")
     * })
     */
    private $unit;
...

当然还有 - 产品。

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

    /**
 * Product
 *
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
 */
class Product
{
    /**
     * @var int
     *
     * @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;

/**
 * @var string
 *
 * @ORM\Column(name="ref", type="string", length=25)
 */
private $ref;

/**
 * @var string
 *
 * @ORM\Column(name="ref4stat", type="string", length=25)
 */
private $ref4Stat;
/**
 * @var int
 *
 * @ORM\Column(name="size", type="integer")
 */
private $size;

/**
 * @ORM\ManyToOne(targetEntity="Unit", inversedBy="products")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="unitid", referencedColumnName="id")
 * })
 */
private $unit;

 /**
 * @ORM\ManyToOne(targetEntity="ProductType", inversedBy="products")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="producttypeid", referencedColumnName="id")
 * })
 */
private $producttype;


/**
 * @ORM\OneToMany(targetEntity="Recipe_Product", mappedBy="product")
*/
private $recipeproducts;
...

编辑食谱时,我希望能够直接添加新的recipe_product系列,但我还没有找到方法......

有人有想法吗?

于 14/10 添加: 我找到了一种呈现表单的方法...... 在我的 easyadmin 配置文件中,我创建了以下条目:

        Recipe:
        class: AppBundle\Entity\Recipe
        form:
            fields:
                - name
                - beer
                - version
                - description
                - createdon
                - { property: 'recipeproducts', label: 'Ingredients', type: 'collection', type_options: {entry_type: 'AppBundle\Form\Recipe_ProductType', by_reference: false} }

表单代码为

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

class Recipe_ProductType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('product')
            ->add('quantity')    
            ->add('unit')
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Recipe_Product'
        ));
    }
}

呈现表单(不会在两个实体之间创建链接,但我猜它必须位于管理 Controller 中)

最佳答案

好的,我找到了解决方案...

请参阅此链接:Symfony 3.0 nested entities not saving

基本上 symfony 生成器没有正确生成 add 函数...

关于symfony easyadmin 一对多形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40007362/

相关文章:

json - grails NullPointerException 保存双向一对多关系模型

symfony2-easyadmin - 如何在 EasyAdmin 的 ListView 中更改添加按钮和搜索标签

symfony - 设置 easyadmin_autocomplete 默认选择 Null。交响乐团

javascript - 显示 2 后在 slider 中显示 1 张图片

php - 使用实体对 Symfony 表单进行单元测试

mysql - Laravel 多对多关系或一对多关系

symfony - 如何在简易管理包中添加重复类型字段

php - PhpStorm IDE 错误后缺少 Symfony/Twig 的模板设置页面

javascript - Symfony2 + Doctrine + FormType,关系和动态添加行

Android:每个表的ContentProvider/处理一对多关系