Search code examples
phpmysqlsymfonydql

Symfony2 createQueryBuilder for One to Many relationship


Now I am trying to write a code for query returns username and content by matching the content id with user id. This is my table looks like.

I have two tables.

  1. One is user table contains username and userid.
  2. The other table is content table which contains content id, content itself, and userid.

I want to write a query something like this:

SELECT username, content 
FROM user 
JOIN ON content 
WHERE user.userid = content.userid

In this way, query returns username with the content that user made. But I have no idea how to do it in Symfony2 or DQL.


Solution

  • Step 1 Create entity User and Content

    For example:

    AppBundle/Entity/User.php

    /**
     * User
     *
     * @ORM\Table(name="user")
     * @ORM\Entity
     */
    class User
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="username", type="string", length=24, nullable=false)
         */
        private $username;
    
        /**
         * @ORM\OneToOne(targetEntity="AppBundle\Entity\Content")
         * @ORM\JoinColumn(name="content_id", referencedColumnName="id")
         **/    
        private $content;
    
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return string
         */
        public function getUsername()
        {
            return $this->username;
        }
    
        /**
         * @param string $username
         */
        public function setUsername($username)
        {
            $this->username = $username;
        }
    
        /**
         * @return \AppBundle\Entity\Content
         */
        public function getContent()
        {
            return $this->content;
        }
    
        /**
         * @param string $content
         */
        public function setContent($content)
        {
            $this->content = $content;
        }
    }
    

    AppBundle/Entity/Content.php

    /**
     * Content
     *
     * @ORM\Table(name="content")
     * @ORM\Entity
     */
    class Content
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="text", type="text", nullable=true)
         */
        private $text;
    
     /**
          * Get id
          *
          * @return integer
          */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return string
         */
        public function getText()
        {
            return $this->text;
        }
    
        /**
         * @param string $text
         */
        public function setText($text)
        {
            $this->text = $text;
        }
    }
    

    Step 2

    Using in controller:

    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository('\AppBundle\Entity\User')->find($userId);
    //content for user
    $content = $user->getContent()->getText();