Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
"related field" behavior in models
  • As started in a precedent post (13264 : i18n in model fields), I would like to define "related fields" in models.

    For example, if I have a
    a model_product which belongs to a model_cat and if I want to define a
    related cat_name field in my product model (where
    model_product->cat_name = model_product->cat->name) I need to
    define It in my model_product AND in my model_cat.

    Other examples :
    * postal code or town of the default address of a client, updated in the client model,
    * client name of a quote, updated in the quote
    * postal code of the client, updated in the quote
    * etc.

    I know this design is
    not recommended because this is voluntary redundant data, but this is
    sometimes very usefull, first when I need to move a field from a model
    to a related one and I don't want to update all my code, but also to
    optimize some database queries or to avoid queries.

    With my knowledge of the ORM I have no other solution to define observers in model_product AND model_cat and I would like to make it easier to implement.

    I think this is a bit complex because I'm
    pretty sure that I need to update observers in related models to check updates in the related field, even if
    the model where the related field is defined is not loaded ! So I would always have to load each model to check if there is a related field defined, or put this complete related fields map in a cache and update it when needed (changing a related field definition).

    I precise I'm not the only one to think of such a thing, fore example in the Odoo (ex openerp, ex-ex tinyerp) ORM, this is native and often used.

    I imagine you will scream or cry after having read this... sorry...
  • What is exactly the problem?

    The relation exists, and "model_product->cat->name" gives you the category name.

    So all you want to do is to map the "cat_name" property to "cat->name"?
  • Yes, exactly. And cp property to client->default_address->cp, and so...
  • That shouldn't be to difficult to implement?

    public function & get($property, array $conditions = array())
    {
        try
        {
            return parent::get($property, $conditions);
        }
        catch (\OutOfBoundsException $e)
        {
            // convert your property here, and re-try the get() call...
        }
    }
  • To get the property this is not so complex ! But I have to change the cat_name value when I change the cat.name value, and this is more complex if I don't want to have to update each related model (as model_cat).

    I just thought I forgot to write that I want to store the related value in the model !
  • You're trying to make the ORM into something it isn't, so you're making it yourself very difficult.

    I can guarantee you you will never get this working properly. Relations are endless, so where does this stop? No to mention recursive relations that will make your model crash if you implement this.

    And all because $model->cat->name is too much work?

Howdy, Stranger!

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

In this Discussion