<bdo id='5LIih'></bdo><ul id='5LIih'></ul>
  1. <i id='5LIih'><tr id='5LIih'><dt id='5LIih'><q id='5LIih'><span id='5LIih'><b id='5LIih'><form id='5LIih'><ins id='5LIih'></ins><ul id='5LIih'></ul><sub id='5LIih'></sub></form><legend id='5LIih'></legend><bdo id='5LIih'><pre id='5LIih'><center id='5LIih'></center></pre></bdo></b><th id='5LIih'></th></span></q></dt></tr></i><div id='5LIih'><tfoot id='5LIih'></tfoot><dl id='5LIih'><fieldset id='5LIih'></fieldset></dl></div>
    1. <legend id='5LIih'><style id='5LIih'><dir id='5LIih'><q id='5LIih'></q></dir></style></legend>
    2. <small id='5LIih'></small><noframes id='5LIih'>

      <tfoot id='5LIih'></tfoot>

      学说:用条件计算实体的项目

      Doctrine: Counting an entity#39;s items with a condition(学说:用条件计算实体的项目)

          <legend id='YC2dX'><style id='YC2dX'><dir id='YC2dX'><q id='YC2dX'></q></dir></style></legend>

          <small id='YC2dX'></small><noframes id='YC2dX'>

            • <bdo id='YC2dX'></bdo><ul id='YC2dX'></ul>
                  <tbody id='YC2dX'></tbody>

              • <tfoot id='YC2dX'></tfoot>

              • <i id='YC2dX'><tr id='YC2dX'><dt id='YC2dX'><q id='YC2dX'><span id='YC2dX'><b id='YC2dX'><form id='YC2dX'><ins id='YC2dX'></ins><ul id='YC2dX'></ul><sub id='YC2dX'></sub></form><legend id='YC2dX'></legend><bdo id='YC2dX'><pre id='YC2dX'><center id='YC2dX'></center></pre></bdo></b><th id='YC2dX'></th></span></q></dt></tr></i><div id='YC2dX'><tfoot id='YC2dX'></tfoot><dl id='YC2dX'><fieldset id='YC2dX'></fieldset></dl></div>
                本文介绍了学说:用条件计算实体的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 Doctrine 中,我如何计算一个实体的物品?例如,我意识到我可以使用:

                How can I count an entity's items with a condition in Doctrine? For example, I realize that I can use:

                $usersCount = $dm->getRepository('User')->count();
                

                但这只会计算所有用户.我想只计算那些具有员工类型的人.我可以这样做:

                But that will only count all users. I would like to count only those that have type employee. I could do something like:

                $users = $dm->getRepository('User')->findBy(array('type' => 'employee'));
                $users = count($users);
                

                这可行,但不是最佳的.是否有类似以下内容:?

                That works but it's not optimal. Is there something like the following:?

                $usersCount = $dm->getRepository('User')->count()->where('type', 'employee');
                

                推荐答案

                好吧,你可以使用 QueryBuilder 设置 COUNT 查询:

                Well, you could use the QueryBuilder to setup a COUNT query:

                假设 $dm 是您的实体经理.

                Presuming that $dm is your entity manager.

                $qb = $dm->createQueryBuilder();
                
                $qb->select($qb->expr()->count('u'))
                   ->from('User', 'u')
                   ->where('u.type = ?1')
                   ->setParameter(1, 'employee');
                
                $query = $qb->getQuery();
                
                $usersCount = $query->getSingleScalarResult();
                

                或者你可以把它写在 DQL:

                Or you could just write it in DQL:

                $query = $dm->createQuery("SELECT COUNT(u) FROM User u WHERE u.type = ?1");
                $query->setParameter(1, 'employee');
                
                $usersCount = $query->getSingleScalarResult();
                

                计数可能需要在 id 字段上,而不是在对象上,无法回忆.如果是这样,只需将 COUNT(u)->count('u') 更改为 COUNT(u.id)>->count('u.id') 或任何你的主键字段.

                The counts might need to be on the id field, rather than the object, can't recall. If so just change the COUNT(u) or ->count('u') to COUNT(u.id) or ->count('u.id') or whatever your primary key field is called.

                这篇关于学说:用条件计算实体的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <small id='oKfLg'></small><noframes id='oKfLg'>

                        <bdo id='oKfLg'></bdo><ul id='oKfLg'></ul>
                          <tfoot id='oKfLg'></tfoot>
                            <tbody id='oKfLg'></tbody>
                          <legend id='oKfLg'><style id='oKfLg'><dir id='oKfLg'><q id='oKfLg'></q></dir></style></legend>

                          <i id='oKfLg'><tr id='oKfLg'><dt id='oKfLg'><q id='oKfLg'><span id='oKfLg'><b id='oKfLg'><form id='oKfLg'><ins id='oKfLg'></ins><ul id='oKfLg'></ul><sub id='oKfLg'></sub></form><legend id='oKfLg'></legend><bdo id='oKfLg'><pre id='oKfLg'><center id='oKfLg'></center></pre></bdo></b><th id='oKfLg'></th></span></q></dt></tr></i><div id='oKfLg'><tfoot id='oKfLg'></tfoot><dl id='oKfLg'><fieldset id='oKfLg'></fieldset></dl></div>