Yii Framework Forum: Issues With Cookbook's Events Examples - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Issues With Cookbook's Events Examples Ch.1 pages 14 & 15

#1 User is offline   Sho Ke 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-May 13

Posted 01 May 2013 - 10:36 AM

I've been coding along with the book without any issues until I got to page 14, where it demonstrates how to send an email notification upon a comment being added to a blog post.

Following the book, I have the following in protected/components/NewCommentEvent.php
class NewCommentEvent extends CModelEvent {
	public $comment;
	public $post;

After that, I added the following to the Post model in protected/models/Post.php. This model was created via Gii, and I left the auto-generated methods intact:
	function addComment(Comment $comment)
		$comment->post_id = $this->id;
		// creating event class instance
		$event = new NewCommentEvent($this);
		$event->post = $this;
		$event->comment = $comment;
		// triggering event
		return $event->isValid;
	// defining onNewComment event
	public function onNewComment($event) 
		// Event is actually triggered here. This way we can use
		// onNewComment method instead of raiseEvent.
		$this->raiseEvent('onNewComment', $event);

Still following the book, I created protected/components/Notifier.php with the following contents:
class Notifier 
	function comment($event)
		$text = "There was a new comment from
		 {$event->comment->author} on post {$event->post->title}";
		mail('idontknowwhattopu@live.com', 'New Comment', $text);

Finally, I created the controller protected/controllers/PostController.php with the following:
class PostController extends CController
	function actionAddComment()
		$post = Post::model()->findByPk(10);
		$notifier = new Notifier();
		//adding the event handler
		$post->onNewComment = array($notifier, 'comment');
		//in production, this should come from $_POST
			$comment = new Comment();
			$comment->author = 'Sam Dark';
			$comment->text = 'Yii events are awesome!';
		//adding the comment

When trying to run this via %siteName%/index.php/Post/AddComment, I get the following error along with a giant stack trace:


PHP warning
include(Comment.php): failed to open stream: No such file or directory

Before the stack trace, it points to line 421 of YiiBase.php:

From the looks if it, I think Yii is trying to find the file Commment.php because of line 13 in PostController.php, which was not defined in the book. Was the class definition of Comment accidentally omitted in the book, or am I just missing something?

Some additional information about the variables used in PostController.php:
var_dump($post) yields:

var_dump($notifier) yields:

Thanks in advance.

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 576
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 01 May 2013 - 10:59 AM

Of course you need to create a model class named Comment to have this to work.
It is mentioned on page 14 somewhere in the middle but should be obvious :)

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users