在 Doctrine2 中将字段聚合到获取的对象

Mol*_*llo 6 php mysql symfony doctrine-orm

编辑:

  • 进一步研究:看起来答案在于更改定制的默认 Hydrator。Doctrine2 允许您仅通过发送他的名字作为参数来更改它:

    $query->getResult('CustomHydrator');

不要忘记先在 config.yml 文件中注册它:

doctrine:
      orm:
         hydrators:
                 CustomHydrator: \your\bundle\Hydrators\CustomHydrator
Run Code Online (Sandbox Code Playgroud)
  • 我的博客和评论实体之间的关系是一对多的。1 个博客有 N 条评论

在研究了如何在 Doctrine 2 中向获取的对象添加额外字段后,我发现了Aggregate Fields,这是一篇很好的文章,但只是谈论从单个帐户中获取余额,它从未说明我们在使用数组时应该做什么帐户,这可能听起来很傻,但让我解释一下我的情况。

就我而言,不是关于帐户和条目,而是关于博客和评论。

我想要做的是列出一些博客,并只显示它有多少评论而不加载任何评论信息,换句话说,我想将此查询转换为 Doctrine2 World。

'SELECT b.*, COUNT( b.id ) AS totalComments FROM  `blogs` b LEFT JOIN comments c ON b.id = c.blog_id GROUP BY b.id LIMIT 8'
Run Code Online (Sandbox Code Playgroud)

我期望的结果是一组正确设置了 totalComments 属性的博客对象,如下所示:

array (size=8)
  0 => 
    object Blog
      'id' => int 330
      'title' => string 'title blog'

      // Added field, not visible in table DB. Came through query COUNT() statement
      'totalComments' => int 5 
      // ... more attributes

  1 => ...
  //more object blogs
  );
Run Code Online (Sandbox Code Playgroud)

我只是无法做到这一点,我能做的最好的是:

创建和获取查询:

$qb = $this->createQueryBuilder('b')
        ->select('b, c')
        ->addSelect('count(b.id) as nComments')
        ->leftJoin('b.comments', 'c')
        ->groupBy('b.id')

        return $qb->getQuery()->getResult();
Run Code Online (Sandbox Code Playgroud)

我得到的结果是一个数组数组,其中位置 0 有博客对象和位置“totalComments”

// var_dump($result)
array (size=8)
  0 => 
    array(2) =>
      0 =>  
          object Blog
          'id' => int 330
          'title' => string 'title blog'
          // ... more attributes

      "totalComments" => int 5

  1 => ...
  );
Run Code Online (Sandbox Code Playgroud)

我也尝试制作自己的 Hydrator,但我刚开始使用 Doctrine2,发现自己有点迷茫。

我希望足够清楚。如果需要,我可以提供任何其他信息。

提前致谢!

Mol*_*llo 1

几天后我想出了这个解决方案。

我必须totalComments向我的博客实体类及其 get/set 方法添加一个属性,并稍微调整一下我的 getLatestBlogs 函数:

function getLatestBlogs(){
    $qb = $this->createQueryBuilder('b')
        ->select('b, c')
        ->addSelect('count(b.id) as totalComments')
        ->leftJoin('b.comments', 'c')
        ->groupBy('b.id');
    $result = $qb->getQuery()->getResult();

    //tweaking original result
    foreach($result as $row){ 
        $row[0]->setTotalComments($row['totalComments']);
        $blogList[] = $row[0];
    }
    return $blogList;
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我最终得到了一个简单的博客对象数组,并且只需要一个额外的循环。

之后,我意识到拥有一个可以与任何实体一起使用的通用函数会很好,所以我创建了下一个函数:

function fixResult($qResult){ //Receives $qb->getQuery()->getResult();
    if(is_array($qResult)){
        $list = array();
        $keys = array_keys($qResult[0]); //Getting all array positions from first row
        $object = $qResult[0][0]; //Getting the actual object fetched
        foreach($keys as $key){ //Searching for existing set methods in the Object
            $method = "set".ucfirst($key);
            if(method_exists($object,$method))
                $methods[$key] = $method;
        }
        foreach($qResult as $row){ //Calling set methods for every row fetched and storing into a new array
            foreach($methods as $key => $met){
                $row[0]->$met($row[$key]);
                $list[] = $row[0];
            }
        }
        return $list;
    }
    else return false;
}
Run Code Online (Sandbox Code Playgroud)

我希望其他人觉得它有用。