How to count total number of rows of table in Cakephp

Patrick picture Patrick · Nov 27, 2013 · Viewed 16.1k times · Source

How to count the total number of rows of table and pass it to the variable? For example, I have product table and there are 10 products in it. I want to count all the rows and get the result 10 and pass it to the variable $rowcount. How can I do it?

Answer

Ma'moon Al-Akash picture Ma'moon Al-Akash · Nov 27, 2013

use find('count')

For example:

$total = $this->Article->find('count');

If you want to use a condition in your query, then pass the conditions array, here is a sample code:

$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));