jeudi 26 juillet 2018

passing recordId in nested realtionship to update form

i create nested relationship by this reference

it works perfectly i can get to two level-deep in nested relationship but the i got i got stuck when try to update data of child of child model.. i dont know to pass parameter which is record id between methods..

so this is the story i 3 models and 1 controller this controller handle main model which has child model, and child model has grand-child model

main 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 $requiredPermissions = ['mismaiti.mywarta.manage_plugins'];

    protected $kebumItemFormWidget;
    protected $updateItemForm;      

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Mismaiti.MyWarta', 'main-menu-item', 'side-menu-rutin');

        $this->kebumItemFormWidget = $this->createKebumItemFormWidget();
        $this->updateItemForm = $this->updateItemFormWidget();
    }    

    public function onLoadCreateItemForm()
    {
        $this->vars['kebumItemFormWidget'] = $this->kebumItemFormWidget;    
        $this->vars['kebaktianId'] = post('manage_id');    
        return $this->makePartial('kebum_item_create_form');
    }

    public function onLoadUpdateItemForm()   
    {
        $this->vars['recordId'] = post('record_id'); //-- USE THIS record_id IN METHOD BELOW 
        $this->vars['updateItemForm'] = $this->updateItemForm;    
        return $this->makePartial('kebum_item_update_form'); 
    }

    protected function updateItemFormWidget()
    {
        //$recordId = post('record_id');   ---> USE record_id HERE 
        $config = $this->makeConfig('$/mismaiti/mywarta/models/kebumitem/kebum_item_fields.yaml');    
        $config->model = \Mismaiti\MyWarta\Models\KebumItem::find($recordId);    
        $widget = $this->makeWidget('Backend\Widgets\Form', $config);    
        $widget->bindToController();    
        return $widget;

    }

    public function onCreateItem()
    {
        $data = $this->kebumItemFormWidget->getSaveData();    
        $model = new \Mismaiti\MyWarta\Models\KebumItem;    
        $model->fill($data);    
        $model->save();    
        $kebaktian = $this->getKebumModel();    
        $kebaktian->kebumitems()->add($model, $this->kebumItemFormWidget->getSessionKey());

        return $this->refreshKebumItemList();
    }

    public function onUpdateItem()
    {
        $id = post('item_id');    
        $model = \Mismaiti\MyWarta\Models\KebumItem::find($id);            
        $data = $this->updateItemForm->getSaveData();       

        $model->fill($data);
        $model->save();     
        \Flash::success('Data has been updated!');
    }

    public function onDeleteItem()
    {
        $recordId = post('record_id');    
        $model = \Mismaiti\MyWarta\Models\KebumItem::find($recordId);

        $kebum = $this->getKebumModel();    
        $kebum->kebumitems()->remove($model, $this->kebumItemFormWidget->getSessionKey());

        return $this->refreshKebumItemList();
    }

    protected function createKebumItemFormWidget()
    {
        $config = $this->makeConfig('$/mismaiti/mywarta/models/kebumitem/kebum_item_fields.yaml');    
        $config->alias = 'kebumItemForm';    
        $config->arrayName = 'KebumItem';

        $config->model = new \Mismaiti\MyWarta\Models\KebumItem;   
        $widget = $this->makeWidget('Backend\Widgets\Form', $config);    
        $widget->bindToController();    
        return $widget;
    }      

    protected function getKebumModel()
    {
        $manageId = post('manage_id');    
        $kebaktian = $manageId
            ? \Mismaiti\MyWarta\Models\Kebaktian::find($manageId)
            : new \Mismaiti\MyWarta\Models\Kebaktian;    
        return $kebaktian;
    }

    protected function refreshKebumItemList()
    {
        $kebumItems = $this->getKebumModel()
            ->kebumitems()
            ->withDeferred($this->kebumItemFormWidget->getSessionKey())
            ->get();    
        $this->vars['kebumItems'] = $kebumItems;    
        return ['#itemList' => $this->makePartial('kebum_item_list')];
    }

}

i put remark in which part that i got stuck, this is where record_id come from

<div class="control-list">
        <table class="table data" data-control="rowlink">
            <thead>
                <tr>
                    <th><span>Jenis</span></th>
                    <th><span>Jam</span></th>
                    <th><span>Pelayan</span></th>
                    <th style="width: 10%"><span></span></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($kebumItems as $item): ?>
                    <tr>
                            <td><a href="javascript:;"
                                   data-control="popup"
                                   data-handler="onLoadUpdateItemForm"                                       
                                   data-request-data="record_id: '<?= $item->id ?>'"
                                   data-size="large">
                                        <?= e($item->jenis) ?></a> 
                            </td>
                            <td><?= e($item->jam) ?></td>
                            <td><?= e($item->pelayan) ?></td>    
                            <td class="nolink text-right">
                                <a
                                    href="javascript:;"
                                    data-request="onDeleteItem"
                                    data-request-data="record_id: '<?= $item->id ?>'"
                                    data-request-confirm="Delete this item?"
                                    class="oc-icon-remove"
                                    data-toggle="tooltip"
                                    title="Remove"></a>
                            </td>

                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
</div> 

i hope someone can help me with this..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire