Php Yii2-使用数组值初始化模型
如何从另一个数组创建具有默认值的模型 我的意思是,如果我有这样一个数组:Php Yii2-使用数组值初始化模型,php,arrays,yii,yii2,Php,Arrays,Yii,Yii2,如何从另一个数组创建具有默认值的模型 我的意思是,如果我有这样一个数组: [ 0 => [ 'remarks' => 'ACETONE - ' 'material' => '70.00' ] 1 => [ 'remarks' => 'Leak Test 1 Bar' 'material' => '13.50' ] 2 => [ 'remarks' => 'Foot Valve Incapsulated O-R
[
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
])] ;
您知道,模型只存储一个默认值,如下所示:
[
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;
}
假设你有
$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;
}
阅读以下内容:
这样,您可以确保仅加载所需的(安全)属性$arrayData是一个模型的键/值对数组。你必须自己迭代。阅读以下内容:
这样,您可以确保仅加载所需的(安全)属性$arrayData是一个模型的键/值对数组。你必须自己迭代