Symfony EasyAdmin一对多表单

7

我是EasyAdmin Bundle的新手,我想知道是否可以直接从父对象添加子对象。所以我有三个对象:

- 食谱 (Recipe)
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;
...

-食谱_产品(具有数量和单位作为属性输入)
   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;
...

在编辑配方时,我希望能够直接添加新的食谱产品行,但是我还没有找到方法...

有人有想法吗?

更新于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'
        ));
    }
}

这个页面渲染的表单(不会创建两个实体之间的链接,但我猜必须在管理员控制器中)。

1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接