我需要在推送到Backpack的store方法中的请求中设置key => value;
在v3中,我有一个像这样的工作存储方法;
Sub OneColumnV2()
Dim iLastcol As Long
Dim iLastRow As Long
Dim jLastrow As Long
Dim ColNdx As Long
Dim ws As Worksheet
Dim myRng As Range
Dim ExcludeBlanks As Boolean
Dim mycell As Range
ExcludeBlanks = (MsgBox("Exclude Blanks", vbYesNo) = vbYes)
Set ws = ActiveSheet
iLastcol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Alldata").Delete
Application.DisplayAlerts = True
Sheets.Add.Name = "Alldata"
For ColNdx = 1 To iLastcol
iLastRow = ws.Cells(ws.Rows.Count, ColNdx).End(xlUp).Row
Set myRng = ws.Range(ws.Cells(1, ColNdx), _
ws.Cells(iLastRow, ColNdx))
If ExcludeBlanks Then
For Each mycell In myRng
If mycell.Value <> "" Then
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
mycell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next mycell
Else
myRng.Copy
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
mycell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next
Sheets("Alldata").Rows("1:1").EntireRow.Delete
ws.Activate
End Sub
但是为了保持开发项目的最新状态,我正在更新到v4,并在尝试使用推荐的traitStore或traitUpdate方法时遇到向$ request对象添加/删除任何内容的问题在文档中。
这不起作用;
public function store(StoreRequest $request) {
$request->request->set('account_type', User::ACCOUNT_TYPE_BASIC);
$redirect_location = parent::storeCrud($request);
return $redirect_location;
}
具体地说,通过traitStore发送到数据库的请求中不包含'account_type'密钥,该请求仅使用在此Crud中(在这种情况下)setupCreateOperation()方法中定义的字段。
这里是否缺少我的东西,还是我需要对需要操纵请求的任何东西进行全面的保存/更新,而不是利用各种背包杂物堆方法?
答案 0 :(得分:1)
问题可能出在v4中。 getStrippedSaveRequest at the bttom of this class 故意删除该属性,因为它不是CRUD面板中的注册字段
/**
* Returns the request without anything that might have been maliciously inserted.
* Only specific field names that have been introduced with addField() are kept in the request.
*/
public function getStrippedSaveRequest()
{
return $this->request->only($this->getAllFieldNames());
}
您可以通过以下方式解决此问题:在CRUD面板中将this属性添加为隐藏字段,如下所示:
$this->crud->addField([
'name' => 'account_type',
'type' => 'hidden'
]);
现在该字段将不会显示在页面上,但是它将被注册,并且在创建过程之前将不再被删除。
答案 1 :(得分:0)
store()不带任何参数,因此您需要将属性直接添加到crud-> request。
此外,您可以动态添加字段,而无需将其创建为隐藏在表单中。
public function store()
{
$this->crud->request->request->add('account_type', User::ACCOUNT_TYPE_BASIC);
$this->crud->addField(['type' => 'hidden', 'name' => 'account_type']);
$response = $this->traitStore();
return $response;
}