Using Yii events

Hi! I am reading this cookbook. I am at page 14:

added class NewCommentEvent


<?php


class NewCommentEvent extends CModelEvent {


    public $comment;

    public $post;


}

changed Post model adding some code:


    public function addComment($comment) {

        if (Yii::app()->params['commentNeedApproval'])

            $comment->status = Comment::STATUS_PENDING;

        else

            $comment->status = Comment::STATUS_APPROVED;

        $comment->post_id = $this->id;


        $event = new NewCommentEvent($this);

        $event->post = $this;

        $event->comment = $comment;

        $this->onNewComment($event);


        return $comment->save();

    }


    public function onNewComment($event) {

        $this->raiseEvent('onNewComment', $event);

    }

added Notifier


<?php


class Notifier {


    function comment($event) {

        $text = "There was new comment from {$event->author} on post {$this->post->title}";

        mail('sensorario@gmail.com', 'New Comment', $text);

    }


}

changed PostController:


    protected function newComment($post) {

        $comment = new Comment;

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'comment-form') {

            echo CActiveForm::validate($comment);

            Yii::app()->end();

        }

        if (isset($_POST['Comment'])) {

            $comment->attributes = $_POST['Comment'];

            if ($post->addComment($comment)) {

                if ($comment->status == Comment::STATUS_PENDING) {

                    // Yii::app()->user->setFlash('commentSubmitted', 'Thank you for your comment. Your comment will be posted once it is approved.');

                    Yii::app()->user->setFlash('commentSubmitted', Yii::t('post.newcomment','Grazie per il tuo commento. Tutti i commenti sono moderati. Sarà visibile solo una volta che sarà approvato.'));

                }

                $this->refresh();

            }

        }


        $notifier = new Notifier();

        $post->onNewComment = array($notifier, 'comment');


        return $comment;

    }

Nothing happen.

Just by looking at your code (did not have the book here)…

In the controller you first call $post->addComment() that in turns raises the event onNewComment()… and only then you set $post->onNewComment to $notifier->comment…

This is correct:

First we have to create the event


<?php


class NewCommentEvent extends CModelEvent {


    public $comment;

    public $post;


}

Secondo, we must tell to Post model to raise event onNewComment when someone add a comment:


    public function addComment($comment) {

        if (Yii::app()->params['commentNeedApproval'])

            $comment->status = Comment::STATUS_PENDING;

        else

            $comment->status = Comment::STATUS_APPROVED;

        $comment->post_id = $this->id;


        $event = new NewCommentEvent($this);

        $event->post = $this;

        $event->comment = $comment;

        $this->onNewComment($event);


        return $comment->save();

    }


    public function onNewComment($event) {

        $this->raiseEvent('onNewComment', $event);

    }

third, in PostController we call the event onNewComment




    protected function newComment($post) {

        $comment = new Comment;

        $notifier = new Notifier();

        $post->onNewComment = array($notifier,'comment');

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'comment-form') {

            echo CActiveForm::validate($comment);

            Yii::app()->end();

        }

        if (isset($_POST['Comment'])) {

            $comment->attributes = $_POST['Comment'];

            if ($post->addComment($comment)) {

                if ($comment->status == Comment::STATUS_PENDING) {

                    // Yii::app()->user->setFlash('commentSubmitted', 'Thank you for your comment. Your comment will be posted once it is approved.');

                    Yii::app()->user->setFlash('commentSubmitted', Yii::t('post.newcomment','Grazie per il tuo commento. Tutti i commenti sono moderati. Sarà visibile solo una volta che sarà approvato.'));

                }

                $this->refresh();

            }

        }

        return $comment;

    }

And finally the Notifier:


<?php


class Notifier {


    function comment($event) {

        $text = "There was new comment from <strong>{$event->comment->content}</strong> on post <strong>{$event->post->title}</strong>";

        mail('sensorario@gmail.com', 'New Comment', $text);

    }


}

This work. It is not very clear, but work. I’ll try later to add some other events.