使用Symfony 3.4和Doctrine在MySql DB上获取不带双倍属性值的不同行

时间:2018-10-23 15:39:04

标签: symfony doctrine-orm doctrine symfony-3.4 symfony3.x

在Symfony 3.4应用程序中,我有一个带有4个属性和ID的实体。

它由mySql DB上的学说管理。

说出分别名为p1,p2,p3和q的属性。 一个示例数据库表可能是这样的:

id  p1  p2  p3  q
------------------
1   x   y   z   1
2   x   y   n   2
3   x       z   1
4   x       z   2
5   x   y   z   3
6   x   y   z   4
7   x   n   z   1

我需要做的是从数据库中请求具有p1,p2,p3不同组合的所有实体。 因此,使用给定的示例数据库表,我需要的结果将是

id  p1  p2  p3  q
------------------
1   x   y   z   1
2   x   y   n   2
3   x       z   1
7   x   n   z   1

因此ID为4、5和6的行将不在集合中,因为p1,p2,p3的组合已“加倍”。

现在-如何使用Symfony 3.4的Repository类上的方法执行此操作,如此处所述:

https://symfony.com/doc/3.4/doctrine.html

或者还有其他方法可以实现我想要的东西吗?

任何提示都非常受欢迎。

编辑: 基本上,我正在查找p1,p2,p3的所有现有组合的列表,而列表中的组合没有加倍。只要包含p1,p2,p3的每种组合的一行(并且只有一行),则返回结果集中的哪一行(关于属性id和q)都没有关系。

2 个答案:

答案 0 :(得分:2)

“分组依据”子查询可消除重复的数据,主查询可从ID中搜索:

$dql = 'SELECT r FROM myTable r
WHERE r.id IN (
    SELECT min(s.id) FROM myTable s
    GROUP BY s.p1, s.p2, s.p3
)';

$rows = $em->createQuery($dql)
    ->getResult();

答案 1 :(得分:1)

如果您只想使用p1p2p3的不同唯一性组合,而不关心idq,则可以排除他们从您的查询,并使用一个独特的条款。

没有您需要的Repository类的内置方法,您可能想创建一个custom repository class(每次我必须为实体编写自定义查询时,我都会亲自进行此操作)

让我们考虑这样声明的您的实体(位于src/AppBundle/Entity/MyEntity.php中的示例):

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
 */
class MyEntity
{
    //declarations of $id, $p1 and so on...
}

您可以使用一种方法来创建自定义存储库类(例如src/AppBundle/Repository/MyEntityRepository.php),以获取所需的结果:

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

class MyEntityRepository extends EntityRepository
{
    public function findAllUniqueCombinations()
    {
        $result = $this->createQueryBuilder('m')
                       ->select('m.p1 p1', 'm.p2 p2', 'm.p3 p3')
                       ->distinct()
                       ->getQuery()
                       ->getResult();

        return ($result);
    }
}

而且,在某些控制器动作中:

$MyCombinationList = $this->getDoctrine()
                          ->getManager()
                          ->getRepository(MyEntity::class)
                          ->findAllUniqueCombinations();

我使用了完全相同的查询进行测试(仅更改了select()

输入数据

id | name       | firstname
---+------------+---------- 
 1 | Smith      | John 
 2 | Smith      | John 
 3 | Smith      | John 
 4 | Smith      | John 
 5 | Doe        | Jane 
 6 | Connor     | Sarah 
 7 | The Sponge | Bob 
 8 | Marley     | Bob

选择为->select('c.name name', 'c.firstname firstname')

输出var_dump($result);给出了:

array (size=5)
  0 => 
    array (size=2)
      'name' => string 'Smith' (length=5)
      'firstname' => string 'John' (length=4)
  1 => 
    array (size=2)
      'name' => string 'Doe' (length=3)
      'firstname' => string 'Jane' (length=4)
  2 => 
    array (size=2)
      'name' => string 'Connor' (length=6)
      'firstname' => string 'Sarah' (length=5)
  3 => 
    array (size=2)
      'name' => string 'The Sponge' (length=9)
      'firstname' => string 'Bob' (length=3)
  4 => 
    array (size=2)
      'name' => string 'Marley' (length=6)
      'firstname' => string 'Bob' (length=3)