-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
ManyToMany relation is not saved to database #860
Comments
I had same problem few weeks ago. This should be define as bidirectional relation Many2Many. /**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Group", inversedBy="users")
* @ORM\JoinTable(name="zu_user_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups; /**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="groups")
*
*/
protected $users;
public function getUsers()
{
return $this->users;
}
public function addUser(User $user)
{
$this->users[] = $user;
$user->addGroup($this);
return $this;
}
public function removeUser(User $user)
{
$this->users->removeElement($user);
$user->removeGroup($this);
} |
@mkalisz77 the mapping is valid for a bidirectionnal relationship. |
@Pierstoval I updated the issue. |
Your mappings seem correct:
But I don't know if it's totally working because the Can you try adding If this does not work, can you show your EasyAdmin configuration? |
It doesn't work. Code: $em = $this->getDoctrine()->getManager();
$c = new Category();
$c->setName('Category 001');
$c->setSlug('category-001');
$c->setContent('category 001');
$p = new Post();
$p->setTitle('Post 001');
$p->setSlug('post-001');
$p->setSummary('Post 001');
$p->setContent('Post 001');
$c->addPost($p);
$em->persist($c);
$em->flush(); Result: config.yml easy_admin:
formats:
date: 'd.m.Y'
time: 'H:i:s'
datetime: 'd.m.Y H:i:s'
list_max_results: 10
entities:
User:
class: AppBundle\Entity\User
Post:
class: AppBundle\Entity\Post
Category:
class: AppBundle\Entity\Category |
I don't know whether it'll solve the issue but I'd suggest changing the // Category.php
/**
* @param Post $post
* @return Category
*/
public function addPost(Post $post)
{
$this->posts[] = $post;
+ if (!$post->getCategories()->contains($this)) {
+ $post->addCategory($this);
+ }
return $this;
} // Post.php
/**
* @param Category $category
* @return Post
*/
public function addCategory(Category $category)
{
$this->categories[] = $category;
+ if (!$category->getPosts()->contains($this)) {
+ $category->addPost($this);
+ }
return $this;
} If this really does not work, you could also try to persist both objects instead of just one. |
@Pierstoval I've tried. This code works, but...
Easyadmin does not work. "update query" does not send. Profiler: |
Have you tested persisting such objects outside EasyAdmin? Maybe something else could be debugged from the profiler 😕 |
I checked. easy-admin-demo have the same problem. 😕 |
I can confirm this before are working and now no Edit: I am getting this problem with a ManyToOne So, this occurs always from a select2 multiple |
I'm pretty sure this has nothing to do with EasyAdmin itself but the Form component + Doctrine. As my wife gave birth a few days ago I don't have time to "code" to test this, I can just advice you to take a look outside EasyAdmin issue tracker (on StackOverflow for instance) to check what's going on. If you find a solution it could be good for you to say it back here 😉 |
@Pierstoval Congratulations 😄 If I find the solution I share here. |
I'm finding the same problem on a M2O relationship. |
Hmmm after some investigation, doesn't seem like add is being called on the related entity, leaving the child entity unlinked. I wonder if this is a doctrine issue. Overriding the admin controller to add a preUpdateEntityNameEntity where I manually link child entity to parent works, like so // src/AppBundle/AdminController.php
/**
* Ensure relations are saved.
*
* @param PortfolioItem $portfolioItem
*/
public function preUpdatePortfolioItemsEntity(PortfolioItem $portfolioItem)
{
foreach ($portfolioItem->getSlideshowItems() as $slideshowItem) {
/** @var SlideshowItem $slideshowItem */
$slideshowItem->setPortfolioItem($portfolioItem);
}
} Without, both entities aren't linked, even though each entity: // Entity\PortfolioItem
/**
* @param SlideshowItem $slideshowItem
*
* @return self
*/
public function addSlideshowItem(SlideshowItem $slideshowItem) : self
{
$this->slideshowItems[] = $slideshowItem;
$slideshowItem->setPortfolioItem($this);
return $this;
} and // Entity\SlideshowItem
/**
* @param mixed $portfolioItem
*
* @return self
*/
public function setPortfolioItem(PortfolioItem $portfolioItem) : self
{
$this->portfolioItem = $portfolioItem;
if ($portfolioItem->getSlideshowItems()->contains($this) === false) {
$portfolioItem->addSlideshowItem($this);
}
return $this;
} |
You need to set by_reference option to false in collection form in Category: - { property: 'posts', type_options: { by_reference: false} } and also as @Pierstoval said modify Category entity: // Category.php
/**
* Add posts
*
* @param \AppBundle\Entity\Post $posts
* @return Category
*/
public function addPost(\AppBundle\Entity\Post $posts)
{
if (!$this->posts->contains($posts)) {
$this->posts[] = $posts;
$posts->addCategory($this);
}
return $this;
}
/**
* Remove posts
*
* @param \AppBundle\Entity\Post $posts
*/
public function removePost(\AppBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
$posts->removeCategory($this);
} this is said in documentation http://symfony.com/doc/current/cookbook/form/form_collections.html
|
Problem is solved. @javierrodriguezcuevas Thanks :) |
Thought I would just add this here, as I was curious about the
http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference |
@javierrodriguezcuevas Thanks! ;) |
I seem to have solved a similar issue that I was having by:
This last point is only needed if trying to persist changes when working on an Entity that is the reverse side.
|
I can add category in the post but can't add post in the category.
Post Entity:
Category Entity:
The text was updated successfully, but these errors were encountered: