One to many : controller & model example

edited March 2021 in Modules

Hello,

I have been using Fuel for a while now, and I just love it !
I think it's a perfect balance for many things.

I am now trying to go to the next level :)

I would like to edit invoice details in the CMS, so for example,
Invoice table :
invoice_id, customer_name, address, city, etc ...
Items table :
item_id, invoice_id, description, quantity, price
(one to many)

In the form_fields of my Invoice_model, I added a template field :

$fields['invoice_items'] = [
    'view'=> '_admin/items',
    'type' => 'template',
    'repeatable' => true,
    'fields' => [
        'description' => [
            'style' => 'width: 400px; text-align:left'
        ],
        'quantity' => [
            'style' => 'width: 40px; text-align:center'
        ],
        'price' => [
            'style' => 'width: 60px; text-align:right'
        ]
    ]
];

and it displays correctly in the create/edit view.

I have several questions :

  • how do I populate this "template" field (and sub fields) with the items from the Items table ? Is a Module the right class for my Invoice_controller ?
  • is it the right thing to do, to save the invoice items in the "on_after_save" of my Invoice_model ?
  • is there a simpler way to populate and save the items that I am missing ? (something similar to the has_many mechanism that would do this automagically) ?

Cheers !
Xavier

Comments

  • 1.1 There is a $values array that can get passed to the template (note the $my_values):

    $fields['invoice_items'] = [
        'view'=> '_admin/items',
        'type' => 'template',
        'values' => $my_values,
        'repeatable' => true,
        'fields' => [
            'description' => [
                'style' => 'width: 400px; text-align:left'
            ],
            'quantity' => [
                'style' => 'width: 40px; text-align:center'
            ],
            'price' => [
                'style' => 'width: 60px; text-align:right'
            ]
        ]
    ];
    

    1.2 Yes, you can do it that way.
    2. Yes, that's what I've done in the past.
    3. That's the preferred way to to do it. There is a normalized_save_data that you can sometimes use to get the submitted data in your on_after_save hook in which to loop through and save:

    public function on_after_save($values)
    {
        parent::on_after_save($values);
        $data = $this->normalized_save_data;
        ....
    }
    
  • Thanks for your quick response !

    I have tried to set $my_values as so :

    $my_values=array();
    $my_values['facture_items'][0]['description']="Banana";
    $my_values['facture_items'][0]['quantity']="1";
    $my_values['facture_items'][0]['price']="50";
    

    but the values are not taken into account, am I doing something wrong ?

  • Are the being passed to the '_admin/items' view in the $values variable?

  • If you mean are they displayed in the view, then no :)

  • edited March 2021

    I looked in the code, and I think I found what needs to be done (no 'facture_items' in the array) :

    $my_values[0]['description']="Banana";
    $my_values[0]['quantity']="1";
    $my_values[0]['price']="450";
    $my_values[1]['description']="Apple";
    $my_values[1]['quantity']="2";
    $my_values[1]['price']="400";
    

    and the name of the variable is 'value'
    'value' => $my_values,

    Thanks for your help, I will try to go further in this functionality. :)

Sign In or Register to comment.