Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

symfony easyadmin one to many form

i am new to the easyadmin bundle and i am looking if it is possible to add childs directly from the parent object So i got 3 objects : - Recipe

namespace AppBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * Recipe
 *
 * @ORMTable(name="recipe")
 * @ORMEntity(repositoryClass="AppBundleRepositoryRecipeRepository")
 */
class Recipe
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

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

-Recipe_Product (which has quantity and unit as attributes to enter)

   namespace AppBundleEntity;

   use DoctrineORMMapping as ORM;

   /**
     * Recipe_Product
     *
     * @ORMTable(name="recipe__product")
     * @ORMEntity(repositoryClass="AppBundleRepositoryRecipe_ProductRepository")
     */
class Recipe_Product
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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


    /**
     * @ORMManyToOne(targetEntity="Recipe", inversedBy="recipeproducts")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="recipeid", referencedColumnName="id")
     * })
     */
    private $recipe;


    /**
     * @ORMManyToOne(targetEntity="Product", inversedBy="recipeproducts")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="Productid", referencedColumnName="id")
     * })
     */
    private $product;

    /**
     * @ORMManyToOne(targetEntity="Unit", inversedBy="recipeproducts")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="Unitid", referencedColumnName="id")
     * })
     */
    private $unit;
...

and of course a - Product.

namespace AppBundleEntity;

use DoctrineORMMapping as ORM;

    /**
 * Product
 *
 * @ORMTable(name="product")
 * @ORMEntity(repositoryClass="AppBundleRepositoryProductRepository")
 */
class Product
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255)
     */
    private $name;

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

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

/**
 * @ORMManyToOne(targetEntity="Unit", inversedBy="products")
 * @ORMJoinColumns({
 *   @ORMJoinColumn(name="unitid", referencedColumnName="id")
 * })
 */
private $unit;

 /**
 * @ORMManyToOne(targetEntity="ProductType", inversedBy="products")
 * @ORMJoinColumns({
 *   @ORMJoinColumn(name="producttypeid", referencedColumnName="id")
 * })
 */
private $producttype;


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

When editing a Recipe, i would like to be able to add directly a new recipe_product line but i haven t found a way to do that...

Anyone has an idea ?

Added on 14/10: I found a way to render the form... in my easyadmin config file, i created the following entry:

        Recipe:
        class: AppBundleEntityRecipe
        form:
            fields:
                - name
                - beer
                - version
                - description
                - createdon
                - { property: 'recipeproducts', label: 'Ingredients', type: 'collection', type_options: {entry_type: 'AppBundleFormRecipe_ProductType', by_reference: false} }

with the Form Code as

<?php

namespace AppBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeHiddenType;

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' => 'AppBundleEntityRecipe_Product'
        ));
    }
}

which renders the form (that don t create the link between the 2 entities but that must be in the Admin controller i guess)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, i found the solution...

see this link: Symfony 3.0 nested entities not saving

Basically symfony generator did not generate the add function correctly...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...