我想用pommbundle创建一个复杂的查询,在单个查询中插入多行是可能的吗?你能给我一个例子吗?
insert into table values (1,1), (1,2), (1,3), (2,1);
例如: Actualy我做到了:
foreach($value['finition'] as $keyFinition => $valueFinition) {
$lot_parametre = $connecteur->getModel(LotparametreModel::class)
->createAndSave(['Lot' => $lot->get('ID'),
'Parametre' =>$keyFinition,
'Valeur' => $valueFinition,
'Niveau' => 2
]);
}
我想要最快速的解决方案
我有一个解决方案: 在控制器中:
$prepare = "";
$valeur = array();
if($value['parametre'] && is_array($value['parametre'])) {
foreach($value['parametre'] as $keyParam => $valueParam) {
//TYPAGE PARAMETRE
$prepare .= "($*::integer, $*::integer, $*::integer, $*::integer),";
$valeur[] = $lot->get('ID');
$valeur[] = $keyParam;
$valeur[] = $valueParam;
$valeur[] = 1;
}
}
//LOT PARAMETRE 2
if($value['finition'] && is_array($value['finition'])) {
foreach($value['finition'] as $keyFinition => $valueFinition) {
//TYPAGE PARAMETRE
$prepare .= "($*::integer, $*::integer, $*::integer, $*::integer),";
$valeur[] = $lot->get('ID');
$valeur[] = $keyFinition;
$valeur[] = $valueFinition;
$valeur[] = 2;
}
}
//INSERTION DES PARAMETRES
if($prepare && $valeur) {
$lot_parametre = $connecteur->getModel(LotparametreModel::class)
->insertMulti(\rtrim($prepare,','),$valeur);
}
/ MODEL / Lotparametremodel.php
public function insertMulti($prepare, $valeur)
{
$sql = "
insert into {lot_parametre}
(\"Lot\", \"Parametre\", \"Valeur\", \"Niveau\")
values
{prepare}
";
$sql = strtr(
$sql,
[
'{lot_parametre}' => $this->structure->getRelation(),
'{prepare}' => $prepare
]
);
return $this->query($sql,$valeur);
}