Yii2 - 使用数组值初始化模型
Yii2 - Initialise a model with array value
如何创建一个具有来自另一个数组的默认值的模型。
我的意思是,如果我有这样的数组:
[
0 => [
'remarks' => 'ACETONE - '
'material' => '70.00'
]
1 => [
'remarks' => 'Leak Test 1 Bar'
'material' => '13.50'
]
2 => [
'remarks' => 'Foot Valve Incapsulated O-Ring 1 pcs - Replace'
'material' => '6.70'
]
3 => [
'remarks' => 'Seal Teflon 3\" Bottom Valve - Replace'
'material' => '10.50'
]
4 => [
'remarks' => 'Gasket Carton Bottom Valve 4 Hole 2 pcs - Replace'
'material' => '14.60'
]
]
你知道,模型只存储一个默认值,如下所示:
$modelJobOrderDetails =[new JobOrderDetail([
'remarks' => ?? get from array above
'material' => ?? get from array above
])] ;
如何将这些数组存储到此模型中?
请指教
假设你有
$myArray = [
0 => [
'remarks' => 'ACETONE - '
'material' => '70.00'
]
1 => [
'remarks' => 'Leak Test 1 Bar'
'material' => '13.50'
]
2 => [
'remarks' => 'Foot Valve Incapsulated O-Ring 1 pcs - Replace'
'material' => '6.70'
]
3 => [
'remarks' => 'Seal Teflon 3\" Bottom Valve - Replace'
'material' => '10.50'
]
4 => [
'remarks' => 'Gasket Carton Bottom Valve 4 Hole 2 pcs - Replace'
'material' => '14.60'
]
];
您可以使用模型属性
遍历您的数组 poplulatin
foreach($mymodel as $key = $value) {
$models[$key] = new JobOrderDetail();
$models[$key]->attributes = $value;
}
http://www.yiiframework.com/doc-2.0/guide-structure-models.html#massive-assignment
读这个:http://www.yiiframework.com/doc-2.0/yii-base-model.html#load()-detail
$model = new JobOrderDetail();
$model->load($arrayData, ''); // '' = formname or empty string
通过这种方式,您可以确保只加载您想要的(安全)属性。 $arrayData 是一个模型的 key/value 对数组。你必须自己迭代。
如何创建一个具有来自另一个数组的默认值的模型。
我的意思是,如果我有这样的数组:
[
0 => [
'remarks' => 'ACETONE - '
'material' => '70.00'
]
1 => [
'remarks' => 'Leak Test 1 Bar'
'material' => '13.50'
]
2 => [
'remarks' => 'Foot Valve Incapsulated O-Ring 1 pcs - Replace'
'material' => '6.70'
]
3 => [
'remarks' => 'Seal Teflon 3\" Bottom Valve - Replace'
'material' => '10.50'
]
4 => [
'remarks' => 'Gasket Carton Bottom Valve 4 Hole 2 pcs - Replace'
'material' => '14.60'
]
]
你知道,模型只存储一个默认值,如下所示:
$modelJobOrderDetails =[new JobOrderDetail([
'remarks' => ?? get from array above
'material' => ?? get from array above
])] ;
如何将这些数组存储到此模型中? 请指教
假设你有
$myArray = [
0 => [
'remarks' => 'ACETONE - '
'material' => '70.00'
]
1 => [
'remarks' => 'Leak Test 1 Bar'
'material' => '13.50'
]
2 => [
'remarks' => 'Foot Valve Incapsulated O-Ring 1 pcs - Replace'
'material' => '6.70'
]
3 => [
'remarks' => 'Seal Teflon 3\" Bottom Valve - Replace'
'material' => '10.50'
]
4 => [
'remarks' => 'Gasket Carton Bottom Valve 4 Hole 2 pcs - Replace'
'material' => '14.60'
]
];
您可以使用模型属性
遍历您的数组 poplulatin foreach($mymodel as $key = $value) {
$models[$key] = new JobOrderDetail();
$models[$key]->attributes = $value;
}
http://www.yiiframework.com/doc-2.0/guide-structure-models.html#massive-assignment
读这个:http://www.yiiframework.com/doc-2.0/yii-base-model.html#load()-detail
$model = new JobOrderDetail();
$model->load($arrayData, ''); // '' = formname or empty string
通过这种方式,您可以确保只加载您想要的(安全)属性。 $arrayData 是一个模型的 key/value 对数组。你必须自己迭代。