【问题标题】:Symfony2 doctrine join returns too much dataSymfony2 教义连接返回太多数据
【发布时间】:2015-02-19 18:35:44
【问题描述】:

我有两个表格 - 类别和主题。以下是这些结构:

/**
 * 论坛分类
 * @ORM\Table(name="forum_categories")
 * @ORM\实体
 */

班级论坛分类
{


    /**
     * @ORM\OneToMany(targetEntity="ForumTopics", mappedBy="category")
     */
    受保护的$主题;

    公共函数 __construct()
    {
        $this->topics = new ArrayCollection();
        $this->children = new ArrayCollection();

    }

    /**
     * @ORM\ID
     * @ORM\Column(type="整数")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    受保护的$id;
    /**
     * @var 字符串
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    私人 $name;

    /**
     * @var 布尔值
     *
     * @ORM\Column(name="top_menu", type="boolean")
     */
    私人 $topMenu;

    /**
     * @var 整数
     *
     * @ORM\Column(name="ord", type="integer", type="decimal", options={"default"="0"})
     */
    私人 $ord;

    /**
     * @var 字符串
     *
     * @ORM\Column(name="color_class", type="string", length=255, nullable=true)
     */
    私人 $colorClass;

    /**
     * @ORM\OneToMany(targetEntity="ForumCategories", mappedBy="parent")
     **/
    私人 $children;

    /**
     * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="children")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     **/
    私人$父母;


    私人 $parentMenu;
    私人 $parentCats;
    /**
     * 设置名称
     *
     * @param 字符串 $name
     * @return 论坛分类
     */
    公共函数 setName($name)
    {
        $this->name = $name;

        返回$这个;
    }

    /**
     * 获取名称
     *
     * @return 字符串
     */
    公共函数 getName()
    {
        返回 $this->name;
    }

    /**
     * 设置顶部菜单
     *
     * @param boolean $topMenu
     * @return 论坛分类
     */
    公共函数 setTopMenu($topMenu)
    {
        $this->topMenu = $topMenu;

        返回$这个;
    }

    /**
     * 获取顶部菜单
     *
     * @return 布尔值
     */
    公共函数 getTopMenu()
    {
        返回 $this->topMenu;
    }

    /**
     * 设置顺序
     *
     * @param 整数 $ord
     * @return 论坛分类
     */
    公共函数 setOrd($ord)
    {
        $this->ord = $ord;

        返回$这个;
    }

    /**
     * 获取订单
     *
     * @return 整数
     */
    公共函数 getOrd()
    {
        返回 $this->ord;
    }

    /**
     * 设置颜色类
     *
     * @param 字符串 $colorClass
     * @return 论坛分类
     */
    公共函数 setColorClass($colorClass)
    {
        $this->colorClass = $colorClass;

        返回$这个;
    }

    /**
     * 获取颜色类
     *
     * @return 字符串
     */
    公共函数 getColorClass()
    {
        返回 $this->colorClass;
    }

    /**
     * 设置父级
     *
     * @param 字符串 $parent
     * @return 论坛分类
     */
    公共函数 setParent($parent)
    {
        $this->parent = $parent;

        返回$这个;
    }

    /**
     * 获取父母
     *
     * @return 字符串
     */
    公共函数 getParent()
    {
        返回 $this-> 父级;
    }



    /**
     * @return 数组集合[]
     */
    公共函数 getChildren()
    {
        返回 $this->children;
    }

    /**
     * @return 数组[int]
     */
    公共函数 getChildrenId()
    {
        $结果 = [];
        foreach ($this->getChildren() as $child) {
            $result[] = $child->getId();
        }
        返回$结果;
    }
    /**
     * 获取身份证
     *
     * @return 整数
     */
    公共函数 getId()
    {
        返回 $this->id;
    }


    /**
     * 添加主题
     *
     * @param \Test\ForumBundle\Entity\ForumTopics $topics
     * @return 论坛分类
     */
    公共函数 addTopic(\Test\ForumBundle\Entity\ForumTopics $topics)
    {
        $this->topics[] = $topics;

        返回$这个;
    }

    /**
     * 删除主题
     *
     * @param \Test\ForumBundle\Entity\ForumTopics $topics
     */
    公共函数 removeTopic(\Test\ForumBundle\Entity\ForumTopics $topics)
    {
        $this->topics->removeElement($topics);
    }

    /**
     * 获取主题
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    公共函数 getTopics()
    {
        返回 $this-> 主题;
    }

和主题:

/** * 论坛分类 * @ORM\Table(name="forum_topics") * @ORM\实体 */ 班级论坛话题 { /** * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="forum_topics") * @ORM\JoinColumn(name="cat_id", referencedColumnName="id") */ 私人 $cat_id; /** * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="forum_topics") * @ORM\JoinColumn(name="cat_id", referencedColumnName="id", nullable=true) */ 私人$类别; /** * @var 整数 * * @ORM\Column(name="user_id", type="integer", type="decimal", options={"default"="0"}) */ 私人 $userId; /** * @ORM\ManyToOne(targetEntity="User", inversedBy="forum_topics") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ 受保护的$用户名; /** * @ORM\OneToMany(targetEntity="ForumPosts", mappedBy="topic_id") */ 公共 $topic_id; 受保护的$posts; 公共函数 __construct() { //父::__construct(); $this->posts = new ArrayCollection(); } /** * @ORM\ID * @ORM\Column(type="整数") * @ORM\GeneratedValue(strategy="AUTO") */ 私人 $id; /** * @var 整数 * * @ORM\Column(name="cat_id", type="integer", type="decimal", options={"default"="0"}) */ 私人 $catId; /** * @var 字符串 * * @ORM\Column(name="name", type="string", length=255) */ 私人 $name; /** * @var \日期时间 * * * @ORM\Column(name="cdate", type="datetime") */ 私人 $cdate; /** * @var \日期时间 * * * @ORM\Column(name="lastpost", type="datetime") */ 私人 $lastpost; /** * 设置catId * * @param 整数 $catId * @return 论坛主题 */ 公共函数 setCatId($catId) { $this->catId = $catId; 返回$这个; } /** * 获取猫号 * * @return 整数 */ 公共函数 getCatId() { 返回 $this->catId; } /** * 设置名称 * * @param 字符串 $name * @return 论坛主题 */ 公共函数 setName($name) { $this->name = $name; 返回$这个; } /** * 获取名称 * * @return 字符串 */ 公共函数 getName() { 返回 $this->name; } /** * 设置用户ID * * @param 整数 $userId * @return 用户 */ 公共函数 setUserId($userId) { $this->userId = $userId; 返回$这个; } /** * 获取用户ID * * @return 整数 */ 公共函数 getUserId() { 返回 $this->userId; } /** * 设置日期 * * @param \DateTime $cdate * @return 论坛主题 */ 公共函数 setCdate($cdate) { $this->cdate = $cdate; 返回$这个; } /** * 获取日期 * * @return \日期时间 */ 公共函数 getCdate() { 返回 $this->cdate; } /** * 设置最后一个帖子 * * @param \DateTime $lastpost * @return 论坛主题 */ 公共函数 setLastpost($lastpost) { $this->lastpost = $lastpost; 返回$这个; } /** * 获取最后一个帖子 * * @return \日期时间 */ 公共函数 getLastpost() { 返回 $this->lastpost; } /** * 获取身份证 * * @return 整数 */ 公共函数 getId() { 返回 $this->id; } /** * 设置类别 * * @param \Test\ForumBundle\Entity\ForumCategories $category * @return 论坛主题 */ 公共函数 setCategory(\Test\ForumBundle\Entity\ForumCategories $category = null) { $this->category = $category; 返回$这个; } /** * 获取猫号 * * @return \Test\ForumBundle\Entity\ForumCategories */ 公共函数 getCategory() { 返回 $this->catId; } /** * 添加帖子 * * @param \Test\ForumBundle\Entity\ForumPosts $posts * @return 论坛主题 */ 公共函数 addPost(\Test\ForumBundle\Entity\ForumPosts $posts) { $this->posts[] = $posts; 返回$这个; } /** * 删除帖子 * * @param \Test\ForumBundle\Entity\ForumPosts $posts */ 公共函数 removePost(\Test\ForumBundle\Entity\ForumPosts $posts) { $this->posts->removeElement($posts); } /** * 获取帖子 * * @return \Doctrine\Common\Collections\Collection */ 公共函数 getPosts() { 返回 $this->posts; } /** * 设置用户名 * * @param \Test\ForumBundle\Entity\User $userName * @return 论坛主题 */ 公共函数 setUserName(\Test\ForumBundle\Entity\User $userName = null) { $this->userName = $userName; 返回$这个; } /** * 获取用户名 * * @return \Test\ForumBundle\Entity\User */ 公共函数 getUserName() { 返回 $this->userName; } /** * 添加topic_id * * @param \Test\ForumBundle\Entity\ForumPosts $topicId * @return 论坛主题 */ 公共函数 addTopicId(\Test\ForumBundle\Entity\ForumPosts $topicId) { $this->topic_id[] = $topicId; 返回$这个; } /** * 删除 topic_id * * @param \Test\ForumBundle\Entity\ForumPosts $topicId */ 公共函数 removeTopicId(\Test\ForumBundle\Entity\ForumPosts $topicId) { $this->topic_id->removeElement($topicId); } /** * 获取topic_id * * @return \Doctrine\Common\Collections\Collection */ 公共函数 getTopicId() { 返回 $this->topic_id; } }

现在我只喜欢使用参数 top_menu = 1 的类别。但我所做的一切都是为我提供带有子类别的类别,其中所有主题都分配给每个类别,所有帖子都分配给每个主题。大量数据。我只想要那些类别名称。这是我的控制器功能:

/**
    * 仅列出顶级类别
    *
    * @Route("/top.{_format}", defaults={"_format"="html"})
    * @Method("GET")
    * @ApiDoc()
    */
    公共函数 listTopAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('TestForumBundle:ForumCategories')->findBy(
            数组('父' => NULL,'topMenu' => '1')

        );

        返回$实体;

    }

    /**
     * 仅列出顶级类别 WDQL
     *
     * @Route("/tops.{_format}", defaults={"_format"="html"})
     * @Method("GET")
     * @ApiDoc()
     */
    公共函数列表TopDQLAction()
    {


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



        $query = $em->getRepository("TestForumBundle:ForumCategories")->createQueryBuilder('c')
            ->where('c.topMenu = 1')
            ->getQuery();

        $results = $query->getResult();
        返回$结果;
    }

有没有办法只获取那些数据我不需要所有其他表中的所有数据通过键组合?感谢您的帮助!

【问题讨论】:

    标签: symfony orm doctrine-orm


    【解决方案1】:

    据我所知,您可能想要这样的东西

    Symfony2 Select one column in doctrine

    但我无法真正理解您想要获得的确切数据结构,以及您指的是控制器中的哪个查询。

    有没有办法只获取那些数据我不需要所有其他表中的所有数据通过键组合?

    也不明白,因为这些查询会返回带有属性的对象而不是带有键的数组。此外,默认情况下,学说不会仅获取单行的所有数据,并且只有当您访问方法时才会要求 db 获取更多数据。

    【讨论】:

      【解决方案2】:

      在这种情况下,我要做的是在 ForumCategories 的存储库中创建一个特定方法,您将在其中返回所需类别的名称。所以你必须在你的bundle的Entity/Repository文件夹中创建一个名为:ForumCategoriesRepository的文件。

      然后你必须以这种方式告诉 ForumCategories 它有一个存储库:

      @ORM\Entity(repositoryClass="{YourNamespacePath}\Entity\Repository\ForumCategoriesRepository")
      

      现在你可以像这样创建一个方法:

      public function getCitiesNames($stateId) {
              $cities = $this->_em->createQueryBuilder()->select('city.name')
                      ->from('\MSD\CommonBundle\Entity\City', 'city')
                      ->where('city.state = :id')
                      ->orderBy('city.name', 'ASC')
                      ->setParameter('id', $stateId)
                      ->getQuery()
                      ->getResult();
      
              return $cities;
          }
      

      然后在你的控制器上调用它:

      $cities = $em->getRepository('MSDCommonBundle:City')
                      ->getCitiesNames($stateId);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多