cake-php@googlegroups.com
[Top] [All Lists]

Re: count(*) how to retrieve?

Subject: Re: count(*) how to retrieve?
From: Dominik Gajewski
Date: Wed, 6 Jul 2011 01:33:10 +0200
I solved this problem. Maybe it is more complicated but it works

$res = $this->DepartmentBook->find('all', array(
        'fields' => array(
                'count(*) AS kl',
                'DepartmentBook.department_id',
                'DepartmentBook.id'
        ),
        'conditions' => array(
                'DepartmentBook.extract_date' => null,
        ),
        'order' => array(
                'DepartmentBook.department_id'
        ),
        'group' => array(
                'DepartmentBook.department_id'
        )
        ));
                
        $res2 = array();
                
        for($i=0; $i<count($res); $i++){
            $res2[$i] = $this->DepartmentBook->find('count', array(
                'fields' => 'DISTINCT DepartmentBook.id',
                'conditions' => array(
                        'DepartmentBook.extract_date' => null,
                        'DepartmentBook.id' => $res[$i]['DepartmentBook']['id']
                ),
                'order' => array(
                        'DepartmentBook.department_id'
                ),
                'group' => array(
                        'DepartmentBook.department_id'
                )
          ));
        }

It is a pity that cake doesn't support the SQL aggregate functions

2011/7/6 Dominik Gajewski <dominikgajewski1@xxxxxxxxx>:
> But 'count' returns only integer of value, and rest column??
>
> And my first sql query returns this
> kl department_id
> 1 4
> 1 8
> 1 10
>
> So only way is to create a own query??
>
> $this->DepartmentBook->query('
> SELECT count( * ) AS kl, ...');
>
>
> 2011/7/6 Tilen Majerle <tilen.majerle@xxxxxxxxx>:
>> instead of "all" use "count" to get number of records from database :)
>> --
>> Lep pozdrav, Tilen Majerle
>> http://majerle.eu
>>
>>
>> 2011/7/6 Dominik Gajewski <dominikgajewski1@xxxxxxxxx>
>>>
>>> Hi
>>>
>>> I would like to count some rows but i can't print results of query
>>> exactly - count(*) AS kl - on the screen.
>>>
>>> this is my query, and there is no error
>>> $res = $this->DepartmentBook->find('all', array(
>>>                                                                'fields' =>
>>> array(
>>>
>>>  'count(*) AS kl',
>>>
>>>  'DepartmentBook.department_id'
>>>                                                                ),
>>>
>>>  'conditions' => array(
>>>
>>>  'DepartmentBook.extract_date' => null,
>>>                                                                ),
>>>                                                                'order' =>
>>> array(
>>>
>>>  'DepartmentBook.department_id'
>>>                                                                ),
>>>                                                                'group' =>
>>> array(
>>>
>>>  'DepartmentBook.department_id'
>>>                                                                )
>>>                ));
>>>
>>> Only when i try to print on screen like that:
>>> for($i = 0; $i < count($res); $i++){
>>>        echo $res[$i]['DepartmentBook']['kl'].'
>>> '.$res[$i]['DepartmentBook']['department_id'].'<br>';
>>> }
>>>
>>> it's showing me this:
>>>
>>> Notice (8): Undefined index:  kl
>>> [APP\views\stats_months\current_graph.ctp, line 13]
>>>
>>> What should I type here $res[$i]['DepartmentBook']['?????']
>>>
>>> --
>>> Dominik Gajewski
>>>
>>> --
>>> Our newest site for the community: CakePHP Video Tutorials
>>> http://tv.cakephp.org
>>> Check out the new CakePHP Questions site http://ask.cakephp.org and help
>>> others with their CakePHP related questions.
>>>
>>>
>>> To unsubscribe from this group, send email to
>>> cake-php+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group
>>> at http://groups.google.com/group/cake-php
>>
>> --
>> Our newest site for the community: CakePHP Video Tutorials
>> http://tv.cakephp.org
>> Check out the new CakePHP Questions site http://ask.cakephp.org and help
>> others with their CakePHP related questions.
>>
>>
>> To unsubscribe from this group, send email to
>> cake-php+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at
>> http://groups.google.com/group/cake-php
>>
>
>
>
> --
> Pozdrawiam
> Dominik Gajewski
>



-- 
Pozdrawiam
Dominik Gajewski

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at 
http://groups.google.com/group/cake-php

<Prev in Thread] Current Thread [Next in Thread>