R
R
Roman Gor2014-12-04 17:07:49
symfony
Roman Gor, 2014-12-04 17:07:49

What is the correct way to make a many-to-many relationship in Symfony?

There are two entities News and User .
When updating the first, in the author field you need to specify the ID of the current user.
I do this one like this:

->add('author', 'hidden', array('data' => $this->securityContext->getToken()->getUser()->getId()))

Description of the author field in the News entity:
/**
     * @var $newsCategory
     *
     * @ORM\ManyToOne(targetEntity="\Sector\UserBundle\Entity\User", inversedBy="id")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="author", referencedColumnName="id")
     * })
     * })
     * @Assert\NotBlank
     */
    private $author;

    /**
     * Set author
     *
     * @param \Sector\UserBundle\Entity\User $author
     */
    public function setAuthor(\Sector\UserBundle\Entity\User $author)
    {
        $this->author = $author;
    }

    /**
     * Get author
     *
     * @return \Sector\UserBundle\Entity\User
     */
    public function getAuthor()
    {
        return $this->author;
    }

But, when updating the form, an error occurs:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Sector\ArticlesBundle\Entity\News::setAuthor() must be an instance of Sector\UserBundle\Entity\User, string given, called in /Users/roman/Development/Sites/Sector.dev/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 377 and defined in /Users/roman/Development/Sites/Sector.dev/src/Sector/ArticlesBundle/Entity/News.php line 341

I checked, in Sector\ArticlesBundle\Entity\News::setAuthor() come the user ID, but, as I understand it, the object should come.
Tell me, please, what is the problem? Where did I miss?
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
spolischook, 2014-12-04
@spolischook

How do you create a form? In a controller, you would normally create something like this:

$news = new News();
$news->setUser($this->getUser());
$form = $this->createForm(new NewsType(), $news);

Thus, the current user will always be detected and it will not be possible to replace him, as in the case of an ID in a hidden field (this is, by the way, a security issue).

P
Petr Zhuchkov, 2014-12-04
@HelsinG

If you do not need to display a field for selecting a user, I would remove the user from the form altogether. And hung up on the creation of news, user installation. For example on prePersist
Here is the list of events.
doctrine-orm.readthedocs.org/en/latest/reference/e...

D
Denis, 2014-12-05
@prototype_denis

In the Many to Many question, in the Many to One code...
Firstly, incorrect annotations, Read the documentation
Secondly, the methods... In theory, for Many to One, the default value is null (setAuthor( $author = null)). The validator will not give you an empty value when submitting the form...
And you don't need to insert it into the form as a hidden field, it's more logical to add the Author before writing directly to the database.
if ($form->isValid()) {
$entity->setAuthor( $this->getUser() );
$entity->flush();
}
In the sonata this is also easy .
If Many To Many, then there must be methods for working with the collection. add, remove, get... As well as joining tables, not columns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question