简体   繁体   English

如何使用CakePHP 2.2.4中的find()计算结果?

[英]How to count the result using find() in CakePHP 2.2.4?

I am using Cakephp 2.2.4 and I need to retrive a list of Lead that belongs to the user ( id = 106 ). 我正在使用Cakephp 2.2.4 ,我需要检索属于该用户的Lead列表( id = 106 )。

The result of the query is: 查询的结果是:

array(
    (int) 0 => array(
        'Lead' => array(
            'id' => '6',
            'user_id' => '106',
            'date' => '2012-12-31 22:15:23',
            'ip' => '127.0.0.1',
            'service_id' => '1',
            'location' => 'Rome',
            'message' => 'Message Message',
            'telephone' => null,
            'active' => null
        ),
        'User' => array(
            'id' => '106',
            'email' => 'daje@daje.it',
            'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
            'role_id' => '3',
            'active' => '1'
        ),
        'Service' => array(
            'id' => '1',
            'name' => 'Primo servizio'
        ),
        'Estimate' => array(
            (int) 0 => array(
                'id' => '1',
                'lead_id' => '6',
                'user_id' => '106'
            )
        )
    )
)

It looks good but I need to count the Estimates (Estimate array), I would like to retrive the number of the estimates, and not the array with all the fields (of estimates table). 看起来不错,但我需要计算估算值(估算数组),我想获取估算值的数量,而不是检索包含所有字段(估算表)的数组。

How can i do it? 我该怎么做?

I need : 我需要 :

Lead array as it shown 如图所示的引线阵列

User array as it shown 如图所示的用户数组

Service array as it shown 如图所示的服务阵列

Estimate (only the total number of the estimates... in this case 1 ) 估计 (在这种情况下,仅估计总数... 1

The find is very simple: 查找非常简单:

$options = array('conditions' => array('User.id' => 106));

debug($this->Lead->find('all', $options));

Try something like this, not 100% sure it'll work but worth a go if not I'd advise trawling the cakephp docs for retrieving your data: 尝试这样的事情,不是100%地确定它会工作,但是如果没有的话,还是值得一试。我建议您拖曳Cakephp文档来检索您的数据:

$options = array(
    'fields' => array('Lead.*', 'User.*', 'Service.*', 'count(Estimate.id)'),
    'conditions' => array('User.id' => 106)
);

Without diving too far into the internals of the Cake ORM, assuming you don't need to do this immediately at query time, couldn't you just get the count of the estimate array programmatically after the fetch? 如果不深入研究Cake ORM的内部结构,假设您不需要在查询时立即执行此操作,那么是否不能仅在获取后以编程方式获取估算数组的count

http://php.net/manual/en/function.count.php http://php.net/manual/zh/function.count.php

$leads = $this->Lead->find('all',$options);
foreach($leads as $lead){
  //get the number of estimates for every lead result
  $lead_num = count($lead['Estimate']);
}

Alternatively, you could manually write a join query for this one fetch and execute it using the Cake Model class's query method. 或者,您可以为此写操作手动编写一个联接查询,并使用Cake Model类的query方法执行它。 Without knowing the specifics of your table schema and model relations its hard to give specifics about how to structure the query, but this shouldn't be too difficult by just look at your table spec and extracting a sql COUNT statement for every Estimate with given id. 在不了解表模式和模型关系的细节的情况下,很难给出关于如何构造查询的细节,但是通过查看表规范并为具有给定id的每个Estimate提取一个sql COUNT语句,这应该不会太困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM