Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to automatically populate my model.
  • Just need to understand more about Orm Models  _init();

    Is there anyway to get the instance in init? I am guessing not since it is static.

    I would like to populate the model's properties with a bunch of values upon instantiation.

    It doesn't look wise to override __construct() as orm\model seems to be doing quite a bit that I don't want to try to redo if it ever changes.

    Can I pass data somehow to back to __construct(array($data) ) from within __init()?

    I know I can simply create a method and call it after it has been instantiated, but I am wondering if this can be automated somehow.

  • The "_init()" method of every class in Fuel is actually no "magic" class as opposed to "__consruct()", but a class that is called by Fuel's Autoloader. That in mind, you cannot do anything as you could within "__construct()" as "_init()" is only being called once i.e., the first time the autoloader looks for the path of the file and includes it.

    What you can do, however, is changing things like a properties default value which you may not be able to in the property definition like e.g., setting some default value to ```time()```.

    If you need to manipulate properties of the model after it was retrieved from the database (or any other place), you might want to look into the \Orm\Observer_Self and the events it has (especially 'after_load') where you can use $this and manipulate the model's properties.
  • You can see _init() as the static equivalent of __construct().

    It was originally designed to have some sort of constructor for classes inside the framework that only have a static interface. They are never instantiated, so a constructor is never called.

    So use it for anything that is static (and that includes ORM model properties), or anything that needs to be done for that particular class only once, and not for every instance. Things like config settings, loading a package or a module, etc...
  • If I try to set the properties using static methods. the properties get set, but _data never does.
    So when I apply to_array() to the object, It returns an empty array.

    I have written observers, but in this case I don't think it will work This model simply builds a lot of data but it does not use a database to store it.  It gets stored as a property (serialized) of another model. Then some more changes get applied on wake_up();

    I suppose I could simply have it as a Non-Orm/Model class.  But there were some methods I wanted to use from Orm.

  • _data isn't static, it's the data container present in any model object. Which means it can't be populated from _init(). Observers also only act on object instances.
  • Ok so it basically looks like the only simple solution here is to create a method to populate the properties and call that every time using the after_create observer.

    It would be nice to have some type of _init() that was called after the class was instantiated automatically since __construct can't practically be overwritten.


  • HarroHarro
    Accepted Answer
    Why not?

    class Blah extends \Orm\Model
    {
        public function __construct(array $data = array(), $new = true, $view = null, $cache = true)
        {
            // do your thing befor constructing

            parent::__construct($data, $new, $view, $cache);

            // do your thing after constructing
        }
    }

    But you don't need to, the model has "after_create" and "after_load" triggers, to which you can attach an observer that modifies every instantiated object (create for new objects, load for objects created from reading a database row).
  • true...   Not so complex after all.   Fo some reason I keep forgetting about calling parent::

    But I think the after_create observer is the way I will go for this.

    Thanks again Harro.


Howdy, Stranger!

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

In this Discussion