【问题标题】:How to use service method in Doctrine QueryBuilder如何在 Doctrine QueryBuilder 中使用服务方法
【发布时间】:2014-03-24 14:25:05
【问题描述】:

我正在做一个相当大的 Symfony2 项目(不幸的是在一个我无法修改结构的数据库上),我被这个困住了:

我有一个用户实体,其中包含(在其他字段中)用户名字段。

我还有一个 ProfileField 实体,它对应于用户的额外字段(如名字或姓氏、喜欢的颜色或您想询问用户的任何内容)。

最后是 ConfigService,它基本上从数据库中获取某个键的某个值。 在这种特殊情况下,这都是关于一个名为“username_format”的小配置值。它可以采用 3 个值之一:“用户名”、“名字”或“名字姓氏”。

根据该值,我需要显示格式正确的用户名。如果值为“用户名” - 我只是从用户实体返回用户名字段值。 对于第 2 和第 3 种情况,因此当我需要获取与该特定用户对应的自定义 ProfileField 时,我创建了一个简单的服务(称为 usernameFormatService),其中注入了 ConfigService 和一个名为 getNameFromId($userId) 的方法。该方法检查配置值并为正确的用户提取正确的值。这一切都很好,但是......

我有一个博客概述页面,其中格式化的用户名显示在其他字段中(如标题、创建日期等)。 Blog 实体与 User 实体有 manyToOne 关系。从映射中,我当然会得到用户名,如果“username_format”配置值表明我需要名字,例如,我将使用 Twig 模板中的 usernameFormatService 将其拉出,一切正常。

当我需要能够按每一列排序时,真正的问题就开始了,这也意味着格式化的用户名列。我正在使用 Doctrine QueryBuilder 来获取数据库结果,基本上我需要用户实体(我认为)内某处的格式化用户名值,以便能够在从数据库中提取博客之前按此值对博客进行排序(为什么之前?分页)。

谁能给我至少提示一下在哪里看或如何做?

更新:

为了更清楚也许:

现在显示在概览表中的用户名正在由 usernameFormatService 解析,它使用 ConfigService 从数据库中获取“platform_username_format”配置值,并根据该配置值返回格式化的用户名。 如果涉及到排序,我需要在实际查询数据库之前以某种方式获取格式化的用户名,以便获得排序结果。

【问题讨论】:

    标签: symfony service doctrine entity


    【解决方案1】:

    好的,如果我在这里理解正确的话,我会做如下所述的事情。

    现在,不确定的是每个用户是否都可以选择如何查看数据。因此,我在服务定义中硬编码了username_format,但这种行为很容易改变。

    1。配置服务

    我会将你的 getNameFromId 更改为 getNameForUser

    class ConfigService{
        private $usernameFormat;
        private $repository;
    
        public function __construct($repository, $usernameFormat){
            $this->repository = $repository;
            $this->usernameFormat = $usernameFormat;
        }
    
        public function getNameForUser(User $user){
            switch ($this->usernameFormat){
                case "username":
                    return $user->getUsername();
                case "firstname":
                    return $user->getFirstName();
                case "firstnamelastname":
                    reteurn $user->getFirstName() . ' ' . $user->getLastname();
            }
        }
    
        public function getAllUsers($page){ 
            return $this->repository->getUsers($page, $this->usernameFormat);
        }
    }
    

    2。配置服务定义

    <parameters>
        <parameter key="my.repository.class">Your\Namespace\FooRepository</parameter>
        <parameter key="config.service.class">Your\Namespace\Service\ConfigService</parameter>
    </parameters>
    
    <services>
        <service id="user.repository"
                 class="%my.repository.class%"
                 factory-service="doctrine.orm.entity_manager"
                 factory-method="getRepository">
                <argument>YourBundle:User</argument>
        </service>
    
        <service id="config.service" class="%config.service.class%">
            <argument type="service" id="my.repository.class" />
            <argument type="string">username</argument>
        </service>
    </services>
    

    3。存储库

    public UserRepository extends EntityRepository{
        public function getUsers($page, $usernameFormat){
            $qb = $this->createQueryBuilder('u');
    
            /**
             * Rest of quering rules go here: WHERE predicate, ORDERing, pagination
             */
    
             return $qb->getQuery()->getResult();   
        }
    }
    

    希望这会有所帮助...

    【讨论】:

    • 你误解了我的意思。为了更清楚地阅读更新的问题。如果还不清楚,请告诉我:)
    猜你喜欢
    • 2014-11-17
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多