Well the strange thing is that "findAfter" is not being called in a
deeply nested association. Also functions calls to the model do get
triggered but I was hoping that the model would contain data retrieved
from the database (instead of just an empty Model "shell").
Here is a good example to illustrate:
Post hasMany Comments
Comment hasOne User
class PostsController extends AppController {
...
function view($id=NULL){
$this->Post->id = $id;
$this->Post->expects(array('PostImage',
'Comment','Comment.User'));
$this->Post->recursive = 2;
$this->set('post', $this->Post->read());
// here I would like to call a function that is associated
with the Users (either in the User Model or UsersController)
// afterFind never gets triggered for the User model (no idea
why...) but it is in view's print_r of the $post
// this calls the model function but the object does not have
any data that was pulled from the db
$this->Post->Comment->User->foo(); // Outputs 'hello my name
is: '
// my noob idea would be that you would have to access it from
the array of Comments like:
foreach ($this->Post->Comment as $comment){
$comment->User->foo();
}
// produces:
// Notice: Trying to get property of non-object
// Fatal error: Call to a member function callMe()
}
} // end Post
class User extends AppModel {
...
function afterFind($result){
print('This never gets called even though there is User data
in the Post's view output');
return $result;
}
function foo(){
print('Hello my name is: ' . $this->User->name);
}
...
}
Thanks for any help with trying to grasp Cake's object creation &
accessing scope.
- John
On May 14, 12:22 am, "Jon Bennett" <[EMAIL PROTECTED]> wrote:
> > I'm wondering what the best practice is for calling a method from
> > inside one controller that happens to associate with another Model/
> > Controller.
>
> > For Instance, if I have a blog post that has many comments and I want
> > to run a method within the CommentsController to format/clean the
> > comment... how would I do this?
>
> > I doubt something like this would work....
>
> > PostController {
> > ...
> > function view($id){
> > ..
> > $this->Post->Comment->cleanComment();
> > }
>
> > Thoughts?
>
> if it's something you'll want after getting any comments (or posts)
> you could create a model method and then use afterfind to format the
> data how you want, does wonders for cutting down on repetitive code.
>
> check out the models section about callbacks in the manual
>
> hth
>
> jb
>
> --
>
> jon bennett
> t: +44 (0) 1225 341 039 w:http://www.jben.net/
> iChat (AIM): jbendotnet Skype: jon-bennett
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---