Hello everyone,
I came across a problem with inserting data into the mysql database.
I'm trying to insert a huge bunch a data rows into the database (talking about 98k single rows). My first approach was this one ($data
is the huge array):
$sql = "INSERT INTO ".$suppdata['supp_tbl']." ($head_fields) VALUES (";
for($i = 1; $i <= $suppdata['num_fields']; $i++)
$sql .= ":tmp".$i.":, ";
$sql = substr($sql, 0, -2).")";
$query = $this->modelsManager->createQuery($sql);
for($i = 0; $i < sizeof($data); $i++)
{
$row = array();
for($j = 0; $j < $suppdata['num_fields']; $j++)
$row['tmp'.($j+1)] = $data[$i][$j];
$status = $query->execute($row);
}
A bit of explaination:
- there are different data sources, so I had to keep the number of binding values dynamic (is this even possible? I'm basically porting a script which was written in "raw" PHP)
- the model name comes from
$suppdata['supp_tbl']
So what I do is to build a query string with the binding parameters :tmpX:
, building up an array for each row, bind it and execute it.
The array which I use to bind the data ($row
) looks fine, and so does the built query ($sql
)
What I get is an error "Artnr is required". "Artnr" is the first field in the table, and also its primary key. I can say for sure it is provided. I even tried to do the "model way", so create an instance of the model, use all the setters and then just $obj->save()
it, but it throws the same error there.
Why do I keep getting this error, although the required data is set?
And by the way, is there a way to see the query the model manager executes, with the data bound?
€dit:
I'm still working on this issue, I even tried it the hard way (just for testing purposes)
$query->execute([
'tmp1' => $data[$i][0],
'tmp2' => $data[$i][1],
'tmp3' => $data[$i][2],
'tmp4' => $data[$i][3],
'tmp5' => $data[$i][4],
'tmp6' => $data[$i][5],
'tmp7' => $data[$i][6],
'tmp8' => $data[$i][7],
'tmp9' => $data[$i][8],
'tmp10' => $data[$i][9],
'tmp11' => $data[$i][10],
'tmp12' => $data[$i][11],
'tmp13' => $data[$i][12],
'tmp14' => $data[$i][13],
'tmp15' => $data[$i][14],
'tmp16' => $data[$i][15]
]);
I checked on the MySQL server, and saw that the query submitted looked like this:
INSERT INTO (table)` (`Artnr`, (other fields)) VALUES (null, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)
Seeing this explains at least the error message, but I don't understand the reason; the array of which I bind the data is populated, but it seems not to bind the values.
Thank you.