Ad

Access Child Model From Parent Controller

I am having trouble accessing child model repeater field to load without user action, I use RelationController in parent model controller this is what I did in parent controller

class WartaRutin extends Controller
{
    public $implement = ['Backend\Behaviors\ListController',
                         'Backend\Behaviors\FormController',
                         'Backend\Behaviors\RelationController']; 

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $relationConfig = 'config_relation.yaml';      

    public function __construct()
    {
        parent::__construct();

    }

    public function formExtendFieldsBefore($form) {

        if($form->model instanceof Mismaiti\MyWarta\Models\Baptis){

            $iteration8 = $form->fields['peserta']['maxItems'];

            if(is_numeric($iteration8) && $iteration8 > 0) {
            $emptyFields = [];                             
                while($iteration8 > 0) {
                    $emptyFields[] = ['anak' => ' '];                    
                    $iteration8--;
                }
            $form->model->peserta = $emptyFields;
            }

        } 
    } 
}

this child model fields

fields:
    ...        
    peserta:
        label: 'Peserta Baptis'
        ...
        maxItems: '4'
        ...
    form:
        fields:
            anak:
                label: 'Nama Anak'
                ...
            ortu:
                ...
            alamatbaptis:
                ...

parent model fields for this

baptisan:
        label: ''           
        ...
        type: partial
        path: field_baptis
        ...

this is config_relation.yaml

baptisans:
label: Baptisan
view:
    list: $/mismaiti/mywarta/models/baptis/relation_columns.yaml
    toolbarButtons: add|create|remove                  
manage:
    form: $/mismaiti/mywarta/models/baptis/relation_fields.yaml
    list: $/mismaiti/mywarta/models/baptis/relation_columns.yaml 

this relation in parent model that i define

public $belongsToMany = [
     'baptisans' => [
        Baptis::class,'table' => 'mismaiti_mywarta_rutbaptis'
     ]
];

relation in child model

public $belongsToMany = [
    'wartarutin' => [
        WartaRutin::class,'table' => 'mismaiti_mywarta_rutbaptis'            
    ]

];

before i use RelationController, this method which is share by @HardikSatasiya work fine, the repeater field autoload base on defined maxItems without user action. can someone share me how to solve this?

Ad

Answer

You can add this code to your WartaRutin controller to add those empty fields

class WartaRutin extends Controller {

    ...

    public function relationExtendManageWidget($widget, $field, $model)
    {
        // we will do it for this model only
        if ($widget->model instanceof \Mismaiti\MyWarta\Models\Baptis) {
            // fetch max item
            $iteration8 = $widget->fields['peserta']['maxItems'];

            // use loop and add those empty fields
            if(is_numeric($iteration8) && $iteration8 > 0) {

                $emptyFields = [];                             
                while($iteration8 > 0) {
                    $emptyFields[] = ['anak' => ' '];                    
                    $iteration8--;
                }
            $widget->model->peserta = $emptyFields;
        }
    }

    ...

Try this it will surely work for you

if any doubt or if it's not working please comment.

Ad
source: stackoverflow.com
Ad