我通过此reference
创建嵌套关系它工作正常,我可以在嵌套关系中达到两个层次,但是当我尝试更新子模型的子模型的数据时,我陷入了困境。我不知道在方法之间传递记录ID的参数。
这就是我有3个相关模型和1个控制器的故事 因此,控制器处理通过关联控制器关联了子模型和关联子模型的主模型,而子模型具有与此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
的{{1}}里面,我就在被卡住的那部分加注,
它运行完美。.
$recordId
这是record_id的来源
$config->model = \Mismaiti\MyWarta\Models\KebumItem::find($recordId);
我希望有人可以帮助我。
答案 0 :(得分:0)
我找到了解决方法。
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();
}