【问题标题】:How to get all possible values of entity field in Symfony 2.8 with Doctrine如何使用 Doctrine 在 Symfony 2.8 中获取实体字段的所有可能值
【发布时间】:2017-04-13 07:19:48
【问题描述】:

假设我有实体User,属性为countrycountry 只是一个字符串,许多用户可以设置相同的country。那么如何获取所有用户的所有唯一国家的列表?我正在使用 Symfony2.8Doctrine

【问题讨论】:

    标签: doctrine-orm properties entity symfony-2.8


    【解决方案1】:

    您需要使用DISTINCT 子句:https://www.w3schools.com/sql/sql_distinct.asp

    以下是您可以通过控制器执行此操作的方法:

     $qb = $em->getRepository("MyBundle:Country")->createQueryBuilder("c");
    
     $countries = $qb->select("c")
        ->distinct(true)
        ->getQuery()
        ->getResult();
    

    一个班轮:

    $countries = $em->getRepository("MyBundle:Country")->findBy(array('distinct' => true));
    

    但是,如果我是你,我会创建一个独特的国家实体,并在用户和国家之间建立多对一关系。这将是一个更清洁的解决方案 IMO

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多