Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm Observers
  • Hello,

    I have one Model named "Model_Post" and another named "Model_Comment".

    Model_Post has the relation "has_many" and Model_Comment has "belongs_to".

    I would like that when I add one comment in my database, the field "nb_comments" of Model_Post be updated by adding +1 to its value.

    If I read good the documentation, I need to use the Observers but I don't know how can I do for my case.

    Thank you in advance for your help
  • HarroHarro
    Accepted Answer
    You get the current model object passed to the observer, so you can do something like

    $model->nb_comments = count($model->comments);

    to store the number of related comments, or

    $model->nb_comments++;

    if you simply want to increment.
  • That I don't understand very well is the system of Observer. Here that I understand about this : I need to create my own Observer, for example "comment.php" stored in fuel/app/classes/observer/comment.php :

    class Observer_Comment extends Orm\Observer
    {

            public function after_save(Orm\Model $model)
            {
                    $model->nb_comments++;
            }
    }



    And in my model comment.php stored in fuel//app/classes/model/comment.php : 

    protected static $_observers = array(
            'Orm\Observer_Comment' => array(
                    'events' => array('after_save')
            ),
    );


    That I don't understand is how my Observer know what model to use ? In my case, $model must be Model_Post but I don't know ho to specify this.
  • HarroHarro
    Accepted Answer
    You need to define the observer on your Model_Post, which is what I assume has the nb_comments counter, not on Model_Comment.

    The ORM will automatically pass the correct object, you don't have to do anything (other then define the observer in the correct place ;).
  • Ok I understand for the object to pass to the Observer. But in my case, I want that when a new comment is saved, the counter nb_comments of Model_Post is updated. So, the Observer is obligatory on my Model_Comment no ? 

    How to listen when a new Model_Comment is added adding an Observer in my Model_Post ?
  • HarroHarro
    Accepted Answer
    No, that won't work, because that observer would only see a single comment, it has no view on the rest, you have to do it from Post.

    $post = Model_Post::find(1);
    $post->comments[] = Model_Comment::forge(array('field' => 'value'));
    $post->save();

    It will save the new comment, trigger the observer on Model_Post, and update the counter.

  • Ok, I understand how it works.

    I've done this in Observer_Post :

    public function before_save(Orm\Model $post)
    {
        $post->nb_comments = count($post->comments);
    }

    This is good, no ?
  • Yes, that should work fine.
  • Ok,

    Thank you very much for your help ! :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion