2016-10-12 44 views
2

我是新來的easyadmin包,我期待它是否可以直接從父對象 增加孩子的,所以我就3個對象: - 配方symfony的easyadmin一對多的形式

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線,但我避風港t上找不到一種方式來做到這一點...

任何人有一個想法?

添加於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' 
     )); 
    } 
} 

這使得形式(即不要噸創建2個實體之間的鏈接,但必須是在管理控制器我想)

回答