简体   繁体   English

Symfony2 createQueryBuilder用于一对多关系

[英]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. 现在,我试图通过将内容ID与用户ID匹配来为查询返回用户名和内容编写代码。 This is my table looks like. 这是我的桌子的样子。

I have two tables. 我有两张桌子。

  1. One is user table contains username and userid . 一种是用户表,其中包含usernameuserid
  2. The other table is content table which contains content id, content itself, and userid . 另一个表是内容表,其中包含内容ID, content本身和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. 但是我不知道如何在Symfony2或DQL中做到这一点。

Step 1 Create entity User and Content 步骤1创建实体用户和内容

For example: 例如:

AppBundle/Entity/User.php 的appbundle /实体/ 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 的appbundle /实体/ 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 第2步

Using in controller: 在控制器中使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM