Search code examples
symfonydoctrinemany-to-one

ManyToOne many Posts for one User -> creates new User by creating a Post


I am using Symfony and Doctrine.

I have two Entities, User and Pots. Every logged User can create as many Post he want to. Every Post is associated to one User.

Thats the Workflow: I log in with an User, this user create a Post. A New Post is saved in the Database with a foreigkey on the User wo create it.

Problem: when the User create the Post, the post ist create, but a new User is created too. The new Post is asssociate to the new Users and not to the logged User.

USER ENTITY:

    namespace AppBundle\Entity;  
    use Doctrine\ORM\Mapping as ORM;
    use AppBundle\Entity\Post;

    /**
    * @ORM\Entity
    * @ORM\Table(name= "User")
    */

    class User 
    {
       /** 
          * @ORM\Column(type = "integer") 
          * @ORM\Id
          * @ORM\GeneratedValue("AUTO") 
       */ 
       private $id;  
        /**
            * @ORM\Column(type = "string", length = 50) 
        */
        private $account;
        /**
        * @ORM\Column(type = "string", length = 22)
        */
        private $password;
        /**
        * @ORM\Column(type = "string", length = 100)
        */
        private $email;
        /**
        * @ORM\Column(type = "integer", length = 1)
        */
        private $type;

        /**
        *
        * @ORM\OneToMany(targetEntity="Post", mappedBy="user")
        */

        private $posts;

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

        /**
         * Set account
         *
         * @param string $account
         *
         * @return User
         */
        public function setAccount($account)
        {
            $this->account = $account;

            return $this;
        }

        /**
         * Get account
         *
         * @return string
         */
        public function getAccount()
        {
            return $this->account;
        }

        /**
         * Set password
         *
         * @param string $password
         *
         * @return User
         */
        public function setPassword($password)
        {
            $this->password = $password;

            return $this;
        }

        /**
         * Get password
         *
         * @return string
         */
        public function getPassword()
        {
            return $this->password;
        }

        /**
         * Set mail
         *
         * @param string $mail
         *
         * @return User
         */
        public function setEmail($email)
        {
            $this->email = $email;

            return $this;
        }

        /**
         * Get mail
         *
         * @return string
         */
        public function getEmail()
        {
            return $this->email;
        }

        /**
         * Set type
         *
         * @param integer $type
         *
         * @return User
         */
        public function setType($type)
        {
            $this->type = $type;

            return $this;
        }

        /**
         * Get type
         *
         * @return integer
         */
        public function getType()
        {
            return $this->type;
        }

        /**
         * Constructor
         */
        public function __construct()
        {
            $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
        }

        /**
         * Add post
         *
         * @param \AppBundle\Entity\Post $post
         *
         * @return User
         */
        public function addPost(\AppBundle\Entity\Post $post)
        {
            $this->posts[] = $post;

            return $this;
        }

        /**
         * Remove post
         *
         * @param \AppBundle\Entity\Post $post
         */
        public function removePost(\AppBundle\Entity\Post $post)
        {
            $this->posts->removeElement($post);
        }

        /**
         * Get posts
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getPosts()
        {
            return $this->posts;
        }
    }

POST ENTITY:

    namespace AppBundle\Entity;  
    use Doctrine\ORM\Mapping as ORM;
    use AppBundle\Entity\User;
    use Symfony\Component\HttpFoundation\File\File;
    use Vich\UploaderBundle\Mapping\Annotation as Vich;



    /**
    * @ORM\Entity
    * @ORM\Table(name = "Post")
    * @Vich\Uploadable
    */

    class Post 
    {

        /**
        * @ORM\Column(type = "integer")
        * @ORM\Id
        * @ORM\GeneratedValue("AUTO")
        */

        private $id;

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

        /**
        * @ORM\Column(type= "string", length = 250)
        */

        private $pic;

        /**
        * @Vich\UploadableField(mapping="post_file", fileNameProperty="pic")
        *
        */

        private $picFile;

        /**
        * @ORM\ManyToOne(targetEntity="User", inversedBy="posts", cascade={"persist"})
        * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
        */

        private $user;



    public function getPicFile(){

        return $this->picFile;

    }

    public function setPicFile(File $picFile = null){

        $this->picFile = $picFile;

        return $this;
    }


        /**
         * Set user
         *
         * @param \AppBundle\Entity\User $user
         *
         * @return Coach
         */
        public function setUser(\AppBundle\Entity\User $user = null)
        {
            $this->user = $user;

            return $this;
        }

        /**
         * Get user
         *
         * @return \AppBundle\Entity\User
         */
        public function getUser()
        {
            return $this->user;
        }

        /**
         * Set title
         *
         * @param string $title
         *
         * @return Post
         */
        public function setTitle($title)
        {
            $this->title = $title;

            return $this;
        }

        /**
         * Get title
         *
         * @return string
         */
        public function getTitle()
        {
            return $this->title;
        }

        /**
         * Set text
         *
         * @param string $text
         *
         * @return Post
         */
        public function setText($text)
        {
            $this->text = $text;

            return $this;
        }

        /**
         * Get text
         *
         * @return string
         */
        public function getText()
        {
            return $this->text;
        }

        /**
         * Set pic
         *
         * @param string $pic
         *
         * @return Post
         */
        public function setPic($pic)
        {
            $this->pic = $pic;

            return $this;
        }

        /**
         * Get pic
         *
         * @return string
         */
        public function getPic()
        {
            return $this->pic;
        }

        /**
         * Set id
         *
         * @param integer $id
         *
         * @return Post
         */
        public function setId($id)
        {
            $this->id = $id;

            return $this;
        }

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

CONTROLLER: NOTE: Here is take the logged User from the SESSION. This works, i output the id from the User i use and it was the correct id.

public function FrameCoachNewAction(Request $request)
    {
        $session = $request->getSession();
        $session->start();
        $user = $session->get('user');

        $post = new Post();
        $form = $this->createForm(PostType::class, $post);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()){

            $post = $form->getData();       
            $post->setUser($user);

            $doct = $this->getDoctrine()->getManager();

            $doct->persist($post);



            $doct->flush();

            //return New Response($post->getUser()->getId());

            return $this->RedirectToRoute('app_frame_coach');
        }else{
            return $this->render('/frame/frame_coach_new.html.twig', array('form' => $form->createView(), 'user' => $user));
        }
    }

Solution

  • I left the Entity like you said, but i changed the Controller to. The Session User Object dind't work to associate it to the Post. So I just toke the ID from the Session and then search the user object again throw that id in the database and used this instade.

    public function FrameCoachNewAction(Request $request)
    {
        $session = $request->getSession();
        $session->start();
        $users = $session->get('user');
    
        $repo = $this->getDoctrine()->getRepository(User::class);
        $user = $repo->find($users->getId());
    
    
    
    
        $post = new Post();
        $form = $this->createForm(PostType::class, $post);
        $form->handleRequest($request);
    
        if($form->isSubmitted() && $form->isValid()){
            $doct = $this->getDoctrine()->getManager();
            $post = $form->getData();       
            $post->setUser($user);
    
            $doct->persist($post);
            $doct->flush();
    
            //return New Response($post->getUser()->getId());
    
            return $this->RedirectToRoute('app_frame_coach');
        }else{
            return $this->render('/frame/frame_coach_new.html.twig', array('form' => $form->createView(), 'user' => $user));
        }
    }