Thursday, April 28, 2011

Custom Find sending back strange results

All I want to do is have one find for my comments in each associated
model. I would love to do this in the Comment model and only have to
do it once but that wasn't working out.

SHOW CONTROLLER

$this->paginate = array('comments', 'id' => $show['Show']['id'],
'model' => 'Show');
$comments = $this->paginate();
$this->set(compact('show', 'comments'));

SHOW MODEL

public $_findMethods = array('comments' => true);

public function _findComments($state, $query, $results=array()) {
if ($state == "before") {
$query = $this->__getComments($query['id'], $query['model'],
$query);
return $query;
} else {
return $results;
}
}

private function __getComments($id, $model, $query) {
$query = ClassRegistry::init('Comment')->find('all', array(
'contain' => array(
'User' => array(
'fields' => array('id', 'username', 'slug'),
'Image' => array(
'fields' => array('name')
)
)
),
'conditions' => array(
'Comment.typeID' => $id,
'Comment.model' => $model
),
'limit' => 5,
'order' => array('Comment.created' => 'desc')
));
return $query;
}

RESULTS

This is the one I want:

SELECT `Comment`.`id`, `Comment`.`parent_id`, `Comment`.`user_id`,
`Comment`.`typeID`, `Comment`.`model`, `Comment`.`comment`,
`Comment`.`created`, `Comment`.`modified`, `User`.`id`,
`User`.`username`, `User`.`slug` FROM `comments` AS `Comment` LEFT
JOIN `users` AS `User` ON (`Comment`.`user_id` = `User`.`id`) WHERE
`Comment`.`typeID` = 2 AND `Comment`.`model` = 'Show' ORDER BY
`Comment`.`created` desc LIMIT 5

This is also on the sql log and it seems to be the one the view is
using:

SELECT `Comment`.`id`, `Comment`.`parent_id`, `Comment`.`user_id`,
`Comment`.`typeID`, `Comment`.`model`, `Comment`.`comment`,
`Comment`.`created`, `Comment`.`modified` FROM `comments` AS `Comment`
WHERE `Comment`.`typeID` IN (2, 3) ORDER BY `Comment`.`created` DESC

I don't even understand where it is coming from because I didn't
define it.

And I get these errors:

Notice (8): Undefined index: page [CORE/cake/libs/model/model.php,
line 2094]
Notice (8): Undefined index: order [CORE/cake/libs/model/model.php,
line 2100]
Notice (8): Undefined index: order [CORE/cake/libs/model/model.php,
line 2103]
Notice (8): Undefined index: callbacks [CORE/cake/libs/model/
model.php, line 2105]
Notice (8): Undefined index: callbacks [CORE/cake/libs/model/
model.php, line 2130]

--
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@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: