Welcome Guest, Not a member yet? Register   Sign In
Problems when doing insert_batch() with objects
#1

[eluser]Razican[/eluser]
Hello, I have the following array, and would like to insert it on my db:

Code:
Array (

[0] => Star Object (
    [galaxy] => 1
    [luminosity] => 0.69879243417539
    [id] => 1
    [orbit] => 25737
    [type] => G
    [mass] => 0.97
    [radius] => 0.98
    [density] => 1451.5560039509
    [temperature] => 5336 )

[1] => Star Object (
    [galaxy] => 1
    [luminosity] => 0.0018724825764223
    [id] => 2
    [orbit] => 22185
    [type] => M
    [mass] => 0.25
    [radius] => 0.26
    [density] => 20033.657929607
    [temperature] => 2357 )

[2] => Star Object (
    [galaxy] => 1
    [luminosity] => 0.00074729502287856
    [id] => 3
    [orbit] => 28025
    [type] => M
    [mass] => 0.39
    [radius] => 0.14
    [density] => 200180.04809125
    [temperature] => 2553 ) ) 1

The problem is that I get the following errors:
Quote:array_keys() expects parameter 1 to be array, object given
database/DB_active_rec.php
Line 1109

sort() expects parameter 1 to be array, null given
database/DB_active_rec.php
Line 1110

and some more related to the fact of using an object instead of an array

I'm using CI 2.1.0

I also get the following MySQL error:

Quote:Column count doesn't match value count at row 1

INSERT INTO `sps_stars` () VALUES (1,0.043148727212324,1,21239,'M',0.22,0.69,943.22585730765,3170), (1,0.035834892964197,2,24368,'M',0.19,0.63,1070.2179772034,3167), (1,0.16953736741583,3,28055,'K',0.51,0.84,1211.9162455667,4045)

Filename: /var/www/space_settler/libraries/Bigbang.php

Line Number: 109

I'm doing it OK? is this a bug or somethig I am doing badly?
#2

[eluser]Razican[/eluser]
I have been reading all the code of the Active Record Class, and have seen that it does not convert object into arrays. This would be the fix, I can do a pull request if you feel OK:

in 2.1.0 line 1109 (line 1238 in 3.0.0-dev-67c287192b5ff414753ae50a834932f676a0db9e)
Change this:
Code:
$keys = array_keys(current($key));
Replace it with this:
Code:
$keys = array_keys($this->_object_to_array(current($key)));

in 2.1.0 line 1114 (line 1243 in 3.0.0-dev-67c287192b5ff414753ae50a834932f676a0db9e)
Add this line:
Code:
$row = $this->_object_to_array($row);

Now it works OK.




Theme © iAndrew 2016 - Forum software by © MyBB