OctoberCMS builder - mass creation of models

133 Views Asked by At

So I have a License model created through the octoberCMS builder with the List and Form views.

The license model contains one relation to School model.

Under the Form view there is a dropdown list with schools and an input field (type=number) which defines how many Licenses to create for the chosen school.

enter image description here

The default behaviour creates only 1 license

How to create the entered amount of licenses instead?

2

There are 2 best solutions below

0
On

You need to override default behaviour.

Note: This task require programming knowledge of OctoberCMS.

In your controller you need to add this method.

use Flash;
use Backend;

// ...

public function create_onSave($context = null)
{

    // 1. init form for your modal and get input data from it
    $model = $this->asExtension('FormController')->formCreateModelObject();
    $model = $this->asExtension('FormController')->formExtendModel($model) ?: $model;
    $this->asExtension('FormController')->initForm($model);
    $form = $this->asExtension('FormController')->formGetWidget();
    $data = $form->getSaveData();

    // 2. get proper count field here and convert to int for loop 
    $count = intval($data['license_to_create']);

    // 3. validation step
    // if($validationFailed) {
    //    Flash::error('Something Went Wrong.');
    //    return;
    // }

    // 4. loop
    foreach ($i = 1; $i <= $count; $i++) {
        $licenseModel = new LicenseModel;
        // you can add other data
        // you can access $data['school_id'] here
        // $licenseModel->school_id = $data['school_id'];
        $licenseModel->save();
    }

    // 5. success message
    Flash::success($count . ' License Added');

    // 6. just redirect it to desired location 
    return Backend::redirect('/hardiksatasiya/sotest/skills');
}

Explanation

  1. here we initialise required variables so we can get data which were filled in text box, this is default code so i just copied it from core code.

  2. once we have our $data variable we can access filled data we use $data['license_to_create'] in your case its 100, and $data['school_id'] for which school you need to create license, Note: you may have different fields please change accordingly.

  3. validation step *optional, you can add some checks here and stop flow if something is not correct with error message.

  4. loop to create new records for license modal ,[ default code will create only 1 record], but here we create it based on given count $data['license_to_create']

  5. just normal success message.

  6. redirect where we need to redirect normally you need to redirect it to /author-name/plugin-name/license-controller Note: you may have different url please change accordingly.

please add comment if you have any doubt.

0
On

Well actually I solved it already also by writing a custom create_onSave function for Licenses controller:

public function create_onSave(){

    $quantity = post('License[_quantity]');
    $school_id = post('License[school]');

    for($i = 1; $i <= $quantity; $i++){
        # Create License
        $license = new \Acme\Plugin\Models\License();
        $license->school_id = $school_id;
        $license->save();
    }

    \Flash::success('Added '.$quantity.' Licenses');

}