Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneToMany update #1971

Closed
AndyCamicci opened this issue Dec 12, 2017 · 3 comments
Closed

OneToMany update #1971

AndyCamicci opened this issue Dec 12, 2017 · 3 comments

Comments

@AndyCamicci
Copy link

AndyCamicci commented Dec 12, 2017

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.

  • a Category Entity
  • a Product Entity
    I have a manyToOne association Product -> Category and a oneToMany Category -> Product.

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
Copy link
Author

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
Copy link

Sorry but I have the same problem and same with

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

Does'nt work.

Any idea ?

@colonelclick
Copy link

This helped me. Thanks.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants