@
@
@koddrive2018-08-10 20:46:53
symfony
@koddrive, 2018-08-10 20:46:53

How to fix the error "Maybe you forget to persist it in the entity manager?" while the form is running entityType?

Didn't find a solution to the error "Entity of type "App\Entity\Books" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?"
There is an entity books

namespace App\Entity;

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

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

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

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Users", mappedBy="books")
     * @ORM\JoinColumn(name="books", referencedColumnName="id")
     */
    private $users;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return Collection|Users[]
     */
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(?Users $user): self
    {
        if (!$this->users->contains($user))
        {
            $this->users[] = $user;
            $user->addBook($this);
        }

        return $this;
    }

    public function removeUser(?Users $user): self
    {
        if ($this->users->contains($user))
        {
            $this->users->removeElement($user);
            $user->removeBook($this);
        }

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }


}

There is a users entity:
namespace App\Entity;

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

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

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Books", inversedBy="users")
     *  @ORM\JoinColumn(name="users", referencedColumnName="id")
     */
    private $books;

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


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Books[]
     */
    public function getBooks(): Collection
    {
        return $this->books;
    }

    public function addBook(?Books $book): self
    {
        if (!$this->books->contains($book)) {
            $this->books[] = $book;
           // $book->addUser($this);
        }

        return $this;
    }

    public function removeBook(?Books $book): self
    {
        if ($this->books->contains($book)) {
            $this->books->removeElement($book);
         //   $book->removeUser($this);
        }

        return $this;
    }
}

There is a ManyToMany relationship between them.
I wanted to make it possible to select several books at once when registering a user through the EntityType form
namespace App\Form;

use App\Entity\Books;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class UserType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('name', TextType::class, [
                'label' => 'Имя',
                'required'   => false,
                'attr' => ['placeholder' => 'Введите имя']
            ])
            ->add('books', EntityType::class, array(
                'class' => Books::class,
                'choice_label' => 'name',
                'label' => 'Книги',
                'multiple' => true
            ))
            ->add('save', SubmitType::class, [
                'label' => 'Отправить'
            ]);
    }

}

But I get the error "Entity of type "App\Entity\Books" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?" and I don't know how to solve it. Who knows how to solve this error, or who knows how to bind ManyToMany to entityType?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2018-08-10
@prototype_denis

You need to use CollectionType with entry_type EntityType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question