active record update_batch() generates errors with stdObjects
I have the following array:
Array ( [0] => stdClass Object ( [_id] => 1 [weight] => 0 )
[1] => stdClass Object
(
[_id] => 5
[weight] => 1
)
[2] => stdClass Object
(
[_id] => 2
[weight] => 2
)
[3] => stdClass Object
(
[_id] => 3
[weight] => 3
)
[4] => stdClass Object
(
[_id] => 4
[weight] => 4
)
) Using this array in the update_batch() function works like a charm, but line 1403 in DB_active_rec.php generates php errors...
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: database/DB_active_rec.php
Line Number: 1403
I know you're supposed to use update_batch with an array of array's, but an array of stdObjects works just as fine, not counting the thrown errors. It seems the line causing the errors isn't even used for anything in the function...
Can you tell what CodeIgniter version you're using and what line 1403 is?
offcourse I can do that. ;)
I am using codeIgniter 2.1.0m the latest version...
In line 1403 of DB_active_rec.php the following happens...
if ($k2 == $index)
{
$index_set = TRUE;
}
else
{
$not[] = $k.'-'.$v; // this is line 1403
}
The variable $not is not used anywhere else in the function set_update_batch(), It must be a remainder of some sort of test or whatnot. Because it is not being used, and the error being thrown is just a warning, the function set_update_batch works just fine with stdObjects. You just see a nasty php error when you're in development mode...
Since I made this post I noticed another similar bug in the function set_insert_batch() of DB_active_rec.php
Lines 1109, 1110, 1114, 1121 of the function set_insert_batch() are causing php errors, when you attempt to pass an array of stdObjects to the function instead of an array of arrays. The lines do the following...
$keys = array_keys(current($key)); //line 1109
sort($keys); //line 1110
foreach ($key as $row)
{
if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) //line 1114
{
// batch function above returns an error on an empty array
$this->ar_set[] = array();
return;
}
ksort($row); // puts $row in the same order as our keys //line 1121
I worked around this without too much trouble by using the function _object_to_array() of DB_active_rec.php on all the elements within my array first before passing it to the insert_batch function.
I know I shouldn't use non-documented functions. but this does save me the trouble of writing said function myself. I think the call to that function should occur in DB_active_rec.php itself, but only when the array contains stdObjects instead of arrays...
Anyway, my reasoning is this. Whe you call a function to retrieve something from the database, you always get stdObjects or an array of stdObjects. I personally like to load these into a webpage, do something with em using jQuery and javascript, and pass em back to my model using ajax calls. Offcourse while doing this these will still be stdObjects, but the functions set_insert_batch() and set_update_batch cannot handle them very well yet...
oops, pressed the wrong button and accidentally closed it. :p
Fixed in the develop branch: https://github.com/EllisLab/CodeIgniter/commit/7b5eb7310e5980ffb23fde8a11261e4a40c3b90e
same issue occurred on version 2.1.3 at line 1407
having this issue too