Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Use of cascade_save into $_belongs_to
  • I have a model Model_Panier (=cart) having manies rows (Model_Panier_Contenu), and I would like to cascade the saves of rows into the cart, so that the total amount and other fields are calculated (using an observer attached to the before_save event).

    My observer is working good, but it is like the the cascade_save does nothing... here is my field declaration :

    class Model_Panier_Contenu extends \Matnat\Model
    {
        ...
        protected static $_belongs_to = array(
            'panier'=>array(
                'key_from'=>'panier_id',
                'model_to'=>'Model_Panier_Panier',
                'cascade_save'=>true,
            ),
           ...
        );
        ...
    }

    when I save the Model_Panier_Contenu it does not cascade save...


  • 'cascade_save' works downwards (parent -> child), not upwards in the relation chain.

    So saving Model_Panier would also save all Model_Panier_Contenu records, but not the other way around.

    And it is on by default, so no need to set it to true. It's there to be able to set it to false, to disable th default behaviour.
  • So, if I want to implement a kind of cascade_save from child to parent, essentially for calculated fields and updated_at value, I imagine that an observer is the right way ..?
  • HarroHarro
    Accepted Answer
    Easier would be to always save the parent. That will trigger any observer on that parent, which could loop over the children and add totals and other stuff), and it will by default cascase_save all children.

    We have a similar setup in our finance module (which has Invoice and Invoicelines models) where the save of the invoice record triggers calculation of VAT on every invoice line, and of the total amount incl. and excl. VAT of the invoice which is stored in the invoice record.

Howdy, Stranger!

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

In this Discussion