【问题标题】:passing recordId in nested relationship to update form在嵌套关系中传递记录 ID 以更新表单
【发布时间】:2019-01-03 08:43:15
【问题描述】:

我通过这个reference创建嵌套关系

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

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

主控制器

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-5 octobercms octobercms-plugins october-form-controller


    【解决方案1】:

    我找到了解决办法..

    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();       
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多