我想在选择带过滤器的对象后插入数据库。
这是我的代码:
var array = [{ name: 'CarNo', attributes: {}, children: [], content: '?' }, { name: 'AccNo', attributes: {}, children: [], content: '?' }, { name: 'SCS', attributes: {}, children: [], content: '?' }],
result = Object.assign(...array.map(o => ({ [o.name]: o.content })));
console.log(result);
选择有效并返回如下对象:
$dayMealType = $newCustomer->Diets()->first()->DietMealPlan()->first()->Days()->filter('Order',$request->postVar('updateData')['dayNr'])->first()->DayMealType()->filter("MealTypeID",$request->postVar('updateData')['MealTypeId'])->first()->Foods();
$food_tmp = new FoodMealPlan();
$food_tmp->Carbohydrates = $request->postVar('updateData')['carbohydrates'];
$dayMealType->add($food_tmp);
$dayMealType->write();
与Foods的课程是这样的:
object(ManyManyList)#102 (16) {
["joinTable":protected]=>
string(17) "DayMealType_Foods"
["localKey":protected]=>
string(14) "FoodMealPlanID"
["foreignKey":protected]=>
string(13) "DayMealTypeID"
["extraFields":protected]=>
array(0) {
}
["_compositeExtraFields":protected]=>
array(0) {
}
["dataClass":protected]=>
string(12) "FoodMealPlan"
["dataQuery":protected]=>
object(DataQuery)#98 (8) {
["dataClass":protected]=>
string(12) "FoodMealPlan"
["query":protected]=>
object(SQLQuery)#97 (12) {
["isDelete":protected]=>
bool(false)
["select":protected]=>
array(0) {
}
["groupby":protected]=>
array(0) {
}
["having":protected]=>
array(0) {
}
["distinct":protected]=>
bool(true)
["orderby":protected]=>
array(0) {
}
["limit":protected]=>
NULL
["where":protected]=>
array(1) {
[0]=>
array(1) {
[""DayMealType_Foods"."DayMealTypeID" = ?"]=>
array(1) {
[0]=>
int(26810)
}
}
}
["connective":protected]=>
string(3) "AND"
["from":protected]=>
array(2) {
["FoodMealPlan"]=>
string(14) ""FoodMealPlan""
["DayMealType_Foods"]=>
array(5) {
["type"]=>
string(5) "INNER"
["table"]=>
string(17) "DayMealType_Foods"
["filter"]=>
array(1) {
[0]=>
string(58) ""DayMealType_Foods"."FoodMealPlanID" = "FoodMealPlan"."ID""
}
["order"]=>
int(20)
["parameters"]=>
array(0) {
}
}
}
["replacementsOld":protected]=>
array(0) {
}
["replacementsNew":protected]=>
array(0) {
}
}
["collidingFields":protected]=>
array(0) {
}
["queriedColumns":"DataQuery":private]=>
NULL
["queryFinalised":"DataQuery":private]=>
bool(false)
["querySubclasses":protected]=>
bool(true)
["filterByClassName":protected]=>
bool(true)
["queryParams":"DataQuery":private]=>
array(3) {
["Component.ExtraFields"]=>
array(0) {
}
["Foreign.ID"]=>
int(26810)
["Foreign.Filter"]=>
array(1) {
[""DayMealType_Foods"."DayMealTypeID""]=>
int(26810)
}
}
}
["model":protected]=>
object(DataModel)#3 (1) {
["customDataLists":protected]=>
array(0) {
}
}
["inAlterDataQueryCall":protected]=>
bool(false)
["failover":protected]=>
NULL
["customisedObject":protected]=>
NULL
["objCache":"ViewableData":private]=>
array(0) {
}
["class"]=>
string(12) "ManyManyList"
["extension_instances":protected]=>
array(0) {
}
["beforeExtendCallbacks":protected]=>
array(0) {
}
["afterExtendCallbacks":protected]=>
array(0) {
}
}
在函数write();
之后返回错误它不会写入DB。
答案 0 :(得分:5)
在添加关系之前,您需要将FoodMealPlan
保存到数据库。
$food_tmp = new FoodMealPlan();
$food_tmp->Carbohydrates = $request->postVar('updateData')['carbohydrates'];
$food_tmp->write(); // Saves to the database
$dayMealType->add($food_tmp);
$dayMealType->write();
参考:https://github.com/silverstripe/silverstripe-framework/blob/3.5/model/ManyManyList.php#L198