1

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.

1 Answers1

2

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.

Protomancer
  • 140
  • 13
  • Actually after looking around there is a function in the controller. The controller gets the first name, last name etc which is coming from the form... –  Feb 09 '15 at 20:56
  • @user4543794 What are you trying to accomplish? Number of posts based on author id? – Protomancer Feb 10 '15 at 07:02
  • Yes from the author id. –  Feb 11 '15 at 18:10
  • @user4543794 You probably want: `$numberOfPosts = count(PostModel::model()->findAll("author_id=$authorId"));` – Protomancer Feb 11 '15 at 22:14