I would like some opinion from seasoned bakers on my implementation as
I am not sure if it is Optimal or as per MVC practice.
Say for eg, I have 2 models Users, Posts.
My desired aim is "In UsersController, I want to find how many posts
current user has posted "
Table Relation
User -> hasMany Post
in PostModel I have
function getPostsCount($userid)
{
return $this->findCount("`Post`.`users_id`='$userid'");
}
Approach 1:
------------------
and for that in User Model have a
function getPostsCount($userid)
{
$this->bindModel(array('hasMany'=>array(
'Post'=>array(
'className'='Post',
'foreignKey'='users_id');
$count = $this->Post->getPostsCount($userid);
$this->unBindModel(array('hasMany'=>array('Post')));
return $count;
}
and in UsersController I get the count by
$postsCount = $this->User->getPostsCount($userid);
Approach 2:
----------------
I do binding the model in the controller itself, so I do not have any
code in Model to wrap around but instead I do in the controller as
follows:
$this->bindModel(array('hasMany'=>array(
'Post'=>array(
'className'='Post',
'foreignKey'='users_id');
$postsCount = $this->User->Post->getPostsCount($userid);
$this->unBindModel(array('hasMany'=>array('Post')));
I prefer Approach 1, but not sure if having the wrapper function is a
good idea or not. What I mean is that getPostsCount is a Post Model
specific function and I create a wrapper in the UserModel to get that
info and don't have bind/unbind the model each time I call such
functions. Moreover I get the ease of use to getPostsCount whereever I
have access to UserModel.
What do you guys have to suggest? I may be going totally against the
principles but not sure, so want to confirm it.
Ketan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---