EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

OneToMany update #1971

Closed AndyCamicci closed 6 years ago

AndyCamicci commented 6 years ago

Hi, first of all, a big thanks for this awesome bundle !

However, something seems not to work properly. I have read #1663, #1447 and #1446 but my problem is slightly different, and none of the solutions worked for me.

I started a Symfony4 project, added Doctrine and EasyAdmin (1.17.7). I have a very (very) basic configuration.

They both are very simple, generated by Make and/or copy/paste from the official documentation here : https://symfony.com/doc/current/doctrine/associations.html

Here is my Category.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     *
     * @return self
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category")
     */
    private $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * @return Collection|Product[]
     */
    public function getProducts()
    {
        return $this->products;
    }

    public function addProduct(Product $product) {
        $product->setCategory($this);
        $this->products->add($product);
        return $this;
    }
    public function removeProduct(Product $product) {
        $product->setCategory(null);
        $this->products->removeElement($product);
        return $this;
    }

    public function __toString() {
        return $this->getName();
    }

}

And my Product.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     *
     * @return self
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products")
     * @ORM\JoinColumn(nullable=true)
     */
    private $category;

    public function getCategory()
    {
        return $this->category;
    }

    public function setCategory(Category $category)
    {
        $this->category = $category;
    }

    public function __toString() {
        return $this->getName();
    }
}

On my easy_admin.yaml config file, i just have

easy_admin:
   entities:
       - App\Entity\Product
       - App\Entity\Category

(Almost) everything works fine, except when i try to edit a category.

Let's say i create a category Cat1, and two products Product1 and Product2, both having Cat1 as the category.

If i edit Cat1, and i try to remove Product2, it doesn't work. If i try to also change the name of the category at the same time, the name get's updated, but not the associated products.

I do have the addProduct and removeProduct functions, i do NOT want to add orphanRemoval=truebecause in this case, it is working but delete completely the product, which is not what i want. I would like to simply set the product's category to null (and yes, the field is nullable).

Can someone help me or point out what i am doing wrong ? Thank you again ! 👍

AndyCamicci commented 6 years ago

Sorry, found a solution, it's in the easy_admin.yaml configuration with the by_reference option

easy_admin:
  entities:
    Category:
      class: App\Entity\Category
      form:
        fields:
          - { property: 'name' }
          - { property: 'products', type_options: { by_reference: false } }
    Product:
      class: App\Entity\Product
CaptainJojo commented 6 years ago

Sorry but I have the same problem and same with

 - { property: 'products', type_options: { by_reference: false } }

Does'nt work.

Any idea ?

colonelclick commented 6 years ago

This helped me. Thanks.

@CaptainJojo What is the name of your field? Is it really products?