Saturday, April 30, 2011

Re: Custom Find sending back strange results

i can tell where the errors come from
in you __getComments you overwrite $query with actual dataset returned
by Comment->find which is wrong
because the $query var is passed to Model->find and it gets confused
with your data

what you need to do is build a proper contain
$query['contain'] = array(
'Comment'=> array(
'User'=>array(....),
'limit'=> <int>
'order'=> 'field name desc'
)
)
return $query

That way you will get your Shows paginated containing comments->userinfo


On Thu, Apr 28, 2011 at 9:07 PM, bradmaxs <brad@bradmaxs.com> wrote:
> 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
>

--
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: