Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to build an Observer for a second ID in EAV Containers
  • Hi there,

    I have the problem that I need to add two IDs for elements which I need to store with EAV Containers: building_id and level_id. With the normal EAV logic I can store the building_id.

    The idea now is, that I set the level_id with an Observer before saving the entries, but that doesn't work in the way I do it now.

    Here is my code so far:

    Controller: http://bin.fuelphp.com/snippet/view/Bk
    Model Levelplan: http://bin.fuelphp.com/snippet/view/Bl
    Model Building: http://bin.fuelphp.com/snippet/view/Bm
    Observer: http://bin.fuelphp.com/snippet/view/Bn

    I have no idea in which of the Models I need to run that Obsever, so I added it actually to both.
    As I also have no experience with writing Observers, I just try to do something, of what might run, but it doesn't do the expected.

    Hope someone can help me letting me know, how I can add this ID correctly via Observer or any other better way.

    Thanks
    Kay


  • Assuming the observer can have some external means of fetching the current level_id, it should be an observer on the EAV model (not on either building or level), that runs on create.
  • Thanks Harro, I now have this Observer running only in the Model Levelplan, which holds the EAV data.

    Also changed before_save to before_insert.

    But how can I now pass the $level_id from my controller to the Observer. I didn't find any similar example in the Observers which are in the system, so have no clue how to do that?

    Can you please give me an example?
    Here is my actual Observer: http://bin.fuelphp.com/snippet/view/Bp
    Here my Levelplan Model: http://bin.fuelphp.com/snippet/view/Bq

    Thank
    Kay
  • If you want to follow best practices, there should not be any dependency between the observer and the model.

    It's probably better to create the EAV objects the old-fashioned way, and not through automatic assignment in the model. So use something like

    $building->levelplans[] = Model_Levelplan::forge(array('level_id' => $level->id, 'key' => 'yourkey', 'value' => yourvalue'));

Howdy, Stranger!

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

In this Discussion