<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>

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

      时间:2024-08-09

          <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.

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

                上一篇:你对 Doctrine ORM 有什么体验? 下一篇:如何创建学说实体的模拟对象?

                相关文章

              • <small id='oKfLg'></small><noframes id='oKfLg'>

                    <bdo id='oKfLg'></bdo><ul id='oKfLg'></ul>
                    <tfoot id='oKfLg'></tfoot>
                  1. <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>