首页 > 解决方案 > 在嵌套关系中传递记录 ID 以更新表单

问题描述

我通过这个引用创建嵌套关系

它工作得很好我可以在嵌套关系中达到两层深度但是当我尝试更新子模型的子数据时我被卡住了..我不知道在方法之间传递参数是记录 id..

所以这就是我有 3 个相关模型和 1 个控制器的故事,所以控制器处理具有子模型并通过关系控制器相关的主模型,而子模型具有与此参考相关的孙子模型

主控制器

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')];
    }

}

我把我卡住的部分注释掉了,如果我把数字放在里面find(42) ,因为它id来自数据库而不是$recordId,它工作得很好..

$config->model = \Mismaiti\MyWarta\Models\KebumItem::find($recordId);

这是 record_id 的来源

<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> 

我希望有人能帮我解决这个问题..

标签: laravel-5octobercmsoctobercms-pluginsoctober-form-controller

解决方案


我找到了解决方案..

protected $kebumItemFormWidget;
protected $updateKebumItemForm;

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

    $this->kebumItemFormWidget = $this->createKebumItemFormWidget();
    //$this->updateItemForm = $this->updateKebumItemFormWidget(); ---> i remove this from here
} 

public function onLoadUpdateKebumItemForm()   
{
    $id = post('record_id');
    Session::put('id',$id);
    $this->vars['id'] = $id;
    $this->vars['updateKebumItemForm'] = $this->updateKebumItemFormWidget(); // declare here instead 
        return $this->makePartial('kebum_item_update_form'); 
}

protected function updateKebumItemFormWidget()
{
    $id = Session::get('id');
    $config = $this->makeConfig('$/mismaiti/mywarta/models/kebumitem/kebum_item_fields.yaml'); 
    $config->model = \Mismaiti\MyWarta\Models\KebumItem::find($id);
    $widget = $this->makeWidget('Backend\Widgets\Form', $config);
    $widget->bindToController();
        return $widget;

}    

public function onUpdateKebumItem()
{
    $id = post('item_id');
    $this->updateKebumItemForm = $this->updateKebumItemFormWidget();
    $model = \Mismaiti\MyWarta\Models\KebumItem::find($id);        
    $data = $this->updateKebumItemForm->getSaveData();        
    $model->fill($data);
    $model->save(); 
    $kebaktian = $this->getKebumModel();
    $kebaktian->kebumitems()->add($model, $this->updateKebumItemForm->getSessionKey());
        \Flash::success('Data has been updated!'); 
        return $this->refreshKebumItemList();       
}

推荐阅读