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 not update, not remove #2015

Closed
CaptainJojo opened this issue Jan 22, 2018 · 12 comments
Closed

OneToMany not update, not remove #2015

CaptainJojo opened this issue Jan 22, 2018 · 12 comments
Labels

Comments

@CaptainJojo
Copy link

But I have the same bug in my project. #1971 #1663 #2004

OneToMany doesn't save.

I have Article Entity

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Dossier", inversedBy="articles", cascade={"persist","remove"})
     */
    private $dossier;

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

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

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

        return $this;
    }

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

    /**
     * @param mixed $dossier
     */
    public function setDossier($dossier)
    {
        $this->dossier = $dossier;

        return $this;
    }

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

And this entity

<?php

namespace App\Entity;

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

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

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Article", mappedBy="dossier", cascade={"persist","remove"})
     */
    private $articles;

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

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

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

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

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

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

    public function addArticle(Article $article) {
        $article->setDossier($this);
        $this->articles->add($article);
        return $this;
    }
    public function removeArticle(Article $article) {
        $article->setDossier(null);
        $this->articles->removeElement($article);

        return $this;
    }
}

My EasyAdmin config it's really easy

easy_admin:
    entities:
        # List the entity class name you want to manage
        Article:
          class: App\Entity\Article
        Dossier:
          class: App\Entity\Dossier
          form:
            fields:
              - 'name'
              - { property: 'articles', type_options: { by_reference: false } }

But when I save in page Dossier, I have every time Article empty.

Any idea ?

@CaptainJojo
Copy link
Author

CaptainJojo commented Jan 22, 2018

For fix I need to overload EntityController and function preUpdateEntity and use my function addArticle for all getArticles.

It's possible without this trick ?

@azvyagin
Copy link

I have the same problem. In issue #1918 says that it works, but not for me.

@azvyagin
Copy link

azvyagin commented Jan 24, 2018

I found a solution to remove, but nor for update
Change:

/**
* @ORM\OneToMany(targetEntity="App\Entity\Article", mappedBy="dossier", cascade="persist","remove"})
*/

in Dossier.php to:

/**
* @ORM\OneToMany(targetEntity="App\Entity\Article", mappedBy="dossier", cascade={"persist","remove"}, orphanRemoval=true)
*/

And change:

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Dossier", inversedBy="articles", cascade={"persist","remove"})
*/

in Article.php to

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Dossier", inversedBy="articles")
*/

This is a special kind of magic...

@laurent-bientz
Copy link
Contributor

I've the same problem since 1.17.8 > updates in children are not saved.

If I force 1.17.7 in composer, still ok.

@javiereguiluz

@grachevko
Copy link
Contributor

I guess problem is here javiereguiluz@6a365fc

@javiereguiluz
Copy link
Collaborator

@grachevko I don't understand why that could be the problem :(

@grachevko
Copy link
Contributor

@javiereguiluz flush with $entity argument perisist only this entity without relations.

@CaptainJojo
Copy link
Author

It's ok with the master branch thank @grachevko I wait the new release :)

@informatic-revolution
Copy link

@javiereguiluz the problem is explained in the following thread: doctrine/orm#4142

@pavbis
Copy link

pavbis commented Feb 3, 2018

@javiereguiluz can confirm that flush with $entity arg persists $entity without relations.
@grachevko thank you, i've updated my project, it's now working.

@javiereguiluz
Copy link
Collaborator

I'm closing this issue because we're starting a new phase in the history of this bundle (see #2059). We've moved it into a new GitHub organization and we need to start from scratch: no past issues, no pending pull requests, etc.

I understand if you are angry or disappointed by this, but we really need to "reset" everything in order to reignite the development of this bundle.

@ahmed-bhs
Copy link

ahmed-bhs commented Dec 15, 2020

Just try to disable by reference on your field https://symfony.com/doc/current/reference/forms/types/choice.html#by-reference

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

No branches or pull requests

8 participants