Tuesday, September 25, 2012

In Model afterFind, calling more than one function fails on second

Here is the code. Can you see what I'm doing wrong? In the afterFind callback, the first replyCount() function call is working just fine, but the subsequent call to lastReply() never returns anything and I don't know why. I can look at the data in MySQL tables and see the data that should be coming back. Is there some issue with calling to private Model functions from each afterFind?

// -----------------------------------------------------------------------------
// PRIVATE FUNCTIONS
    private function replyCount($id) {
        return $this->find('count', array(
                    'conditions' => array(
                        'Post.deleted !=' => 1,
                        'Post.replyto_post_id' => $id
                    )
               ));
    }
 
    private function lastReply($id) {
        $reply = $this->find('all', array(
            'conditions' => array(
                'Post.deleted !=' => 1,
                'Post.replyto_post_id' => $id),
            'order' => array('activitydate DESC'),
            'limit' => 1
        ));
        $returnVal = "";
        if ( count($reply) > 0 ) {
            $returnVal = $reply['PostedBy']['tag']."<br /><small>".$reply['Post']['activitydate']."</small>";
        }
        return $returnVal;
    }

// -----------------------------------------------------------------------------
// CALLBACKS
    public function afterFind($results, $primary = false) {
        parent::afterFind($results,$primary);
        foreach ($results as $key => $val) {
            if (isset($val['Post']['id'])) {
                $results[$key]['Post']['replies'] = $this->replyCount($val['Post']['id']);
                $results[$key]['Post']['lastreply'] = $this->lastReply($val['Post']['id']);
            }
        }
        return $results;
    }




--
Like Us on FacekBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: