Yii Framework Forum: Count on a findAll query - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Count on a findAll query Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,049
  • Joined: 30-October 09

Posted 23 July 2010 - 05:38 PM

Sorry this is probably a noob question but I'm trying to retrieve the number of rows returned in a query.

One of my model functions runs a findAll() query - is it possible to get the number of records in the resultset?

public function myMethod()
{
	return CHtml::listData(Town::model()->findAll(array(*query criteria here*)), 'id', 'name');
}


I had a look at the CActiveRecord count() method but this actually gave me an incorrect count. I then used the normal count() method, as in count($model->myMethod) but I assume this will run the query again.

So what is the best way to do this?
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,523
  • Joined: 12-October 09
  • Location:Croatia

Posted 24 July 2010 - 11:16 AM

Haven't tested but as per documentation (http://www.yiiframew...#findAll-detail) findAll returns an array so you can count the number of array elements perhaps... (http://php.net/manua...ction.count.php)
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   yiimann 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 19-December 09

Posted 24 July 2010 - 04:57 PM

what about ?
$results = Town::model()->findAll(array(*query criteria here*));
$count = count ( $results )

3

#4 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,049
  • Joined: 30-October 09

Posted 24 July 2010 - 05:42 PM

View Postyiimann, on 24 July 2010 - 04:57 PM, said:

what about ?
$results = Town::model()->findAll(array(*query criteria here*));
$count = count ( $results )


Hmmm that would probably be the ideal solution, but as per my original post I am using listData within a model function to return the resultset, and I'm not sure how I would modify it to work in that way.
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,523
  • Joined: 12-October 09
  • Location:Croatia

Posted 25 July 2010 - 05:56 AM

be creative... Posted Image

you can for example in myMethod return the findAll result... like

return Town::model()->findAll(array(*query criteria here*));


then on the call you can do something like

$data=myMethod();

$count=count($data);
$list=CHtml::listData($data);

Find more about me.... btw. Do you know your WAN IP?
2

#6 User is offline   Jay Haase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 21-May 11

Posted 21 August 2011 - 05:45 AM

I think this way of retrieving the count of a query might be a bit more efficient:

$sql = "SELECT COUNT(*) as clients FROM client_table";
$command = Yii::app()->db->createCommand($sql);
$results = $command->queryAll();
$numClients = (int)$results[0]["clients"];


Returning all of the results in an array, and then counting the array elements can be intensive if many rows were returned.
0

#7 User is offline   speedyfw 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 22-June 11
  • Location:Hof, Germany

Posted 26 September 2011 - 09:51 AM

$sql = "SELECT COUNT(*) FROM client_table";
$numClients = Yii::app()->db->createCommand($sql)->queryScalar();


The best way. (as far as i know :) )
3

#8 User is offline   radames_ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 06-October 11
  • Location:São Paulo - Brazil

Posted 06 February 2012 - 10:54 AM

Hello there!

I want to make my contribution ...

$count = Table::Model()->count("field=:field", array("field" => $fildID));
echo $count;


This is a simple way to display the value!

that's all folks!
Radamés Galvao
PHP with Yii devel.
2

#9 User is offline   Azhar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 24-September 12

Posted 20 January 2014 - 03:51 AM

View Postradames_, on 06 February 2012 - 10:54 AM, said:

Hello there!

I want to make my contribution ...

$count = Table::Model()->count("field=:field", array("field" => $fildID));
echo $count;


This is a simple way to display the value!

that's all folks!


Also if you want to add Some conditions like group by etc.
then you can use it like
$criteria = new CDbCriteria();
$criteria->condition = 'field = '.$fildID;
$criteria->group = 'UserId';
$count = Table::Model()->count($criteria);
echo $count;

0

#10 User is offline   scottreainc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 15-October 12

Posted 07 November 2014 - 11:24 AM

Just my little contribution to elaborate on count.
There is a countByATtributes which is similar to findAllByAttributes etc.

Usage:
//get all sales employees not on leave
$total_sales_employees=Employee::model()->countByAttributes(array(
			'department'=>'sales',
			'onleave'=>0
		));

0

#11 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 19-April 13
  • Location:iran

Posted 07 November 2014 - 11:31 PM

what way is
Fastest if yuor db have 20.000.000 record ??!!!
0

#12 User is offline   georaldc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 08-July 11

Posted 09 November 2014 - 04:50 AM

View Postn-r, on 07 November 2014 - 11:31 PM, said:

what way is
Fastest if yuor db have 20.000.000 record ??!!!

Probably try vertical partitioning or if that isn't possible, create a new table that would store counts for you
0

#13 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 266
  • Joined: 19-April 13
  • Location:iran

Posted 09 November 2014 - 05:00 AM

View Postgeoraldc, on 09 November 2014 - 04:50 AM, said:

Probably try vertical partitioning or if that isn't possible, create a new table that would store counts for you

thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users