Reputation:
I'm new to the Yii Framework. In fact, it was dumped on my lap last week. On the email confirmation I need to print "number" of posts. For example, 3 posts.
The problem I'm not sure how to count the posts and print it on the confirmation. Do I add the db query to the controller instead of the form? And then print out the number of posts like below? -
<?php echo $posts->count($posts);
I'm just a little confused using a framework. I need a little push.
Thank you in advance.
Upvotes: 1
Views: 90
Reputation: 139
It's been quite a while since I have used yii so I'm a little rusty.
I'm going to make an assumption: $post
is extended from CActiveRecord
the count function being used by your CActiveRecord requires a sql condition (noted here).
My next assumption is that you are preparing all this business logic in your view and not in the controller. You might consider sending from the view to a decorator, for your email confirmation message.
This is untested, but below we need to compare all the pk of posts:
echo $posts->count('fieldDate < now() AND fieldDate >' . strtotime('yesterday'));
Better and easier might be:
echo $posts->countByAttributes(array('postID'));
If you are looking to count all posts by an author (also untested):
$numberOfPosts = count(PostModel::model()->findAll("author_id=$authorId"));
Otherwise you might try:
count($posts);
If you are receiving an array of post objects that have be queried in a previous controller in the view.
Fat model, thin controller.
Upvotes: 2