Harsha M V
Harsha M V

Reputation: 54949

CakePHP Find function

i am using the following statement.

$users = $this->User->find('all');

But in the database there are only 174 rows. but the query is returning 200 rows.

When i out put the content i see that a lot of rows are repeated.

Any idea why this behavior in cakephp ?

enter image description here

Structure

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(40) NOT NULL,
  `display_photo` varchar(255) DEFAULT NULL,
  `subscription_plan_id` int(11) unsigned NOT NULL,
  `company_id` int(11) NOT NULL,
  `status` tinyint(2) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=177 ;

by trying to debug using this statment

echo $users = $this->User->find('count');

i get 200 itself.

Models: http://pastebin.com/p4bFPiUz

Upvotes: 2

Views: 388

Answers (1)

Oldskool
Oldskool

Reputation: 34837

The query you are running does not nearly match the queries that CakePHP will actually execute. CakePHP will also perform all the required joins to get related data. So, this is not really a proper comparison that you are doing.

Some of your relations might return double results, like User -> CompanyA, but there could also be a User -> CompanyB relation, which would trigger 2 result rows for 1 single user.

To see the queries that Cake actually executes, use the getLog method on your datasource, like:

$ds = $this->User->getDataSource();
$log = $ds->getLog();
debug($log);

Or use something like DebugKit to get a panel with all the queries by default (when in debug mode).

Upvotes: 2

Related Questions