4
votes

I would like to import data from a JSON file to a database table. I'm using Doctrine with the pdo_sqlite driver and the following entity is configured :

/**
 * @Entity @Table(name="mytable")
 **/
class MyClass
{
    /** @Id @Column(type="integer") @GeneratedValue *
     * @Column(type="integer")
     */
    var $id;

    /** @Column(type="string") **/
    var $name;


    /** @Column(type="simple_array") **/
    var $parameters;

    function __construct($name, $parameters)
    {
        $this->name = $name;
        $this->parameters = $parameters;
    }

    // getters and setters here
}

I have created a simple import method :

function importFromJson($tableName, $fileName)
    {
        if (file_exists($fileName)) {
            $data = json_decode(file_get_contents($fileName));
            if (is_array($data)) {
                $connection = getEm()->getConnection();
                foreach($data as $tuple) {
                    if (is_object($tuple)) {
                        $connection->insert($tableName, (array)$tuple);
                    }
                }
            }
        }
    }

My import.json file contains the following text :

[
  {
    "name": "A name",
    "parameters": ["a","b","c"]
  }
]

When I call my import method :

importFromJson("mytable", "import.json");

I get the following error :

An exception occurred while executing 'INSERT INTO mytable (name, parameters) VALUES (?, ?)' with params ["A name", ["a","b","c"]]:

Array to string conversion C:\myproject\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:119 C:\myproject\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php:996 C:\myproject\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php:696 C:\myproject\lib\import.php:39

Isn't the exact purpose of the simple_array type to convert arrays to strings that can be stored in the database ?

1
If you need to store array data, try json_encode($array) function. This will convert your array to string. Then while retrieving data, call json_decode($string) function.Tomas Votruba
I see. What happens if you try setting simple empty array?Tomas Votruba
The same error appears, same thing if I use the "array" type instead of "simple_array". Even more strange : if I use the type "object" and I replace the JSON file by the following content : [ { "name": "A name", "parameters": {} } ] I get the error "Object of class stdClass could not be converted to string"Bruno Pérel
For clarity, which is the version of Doctrine you are using?Aerendir

1 Answers

-1
votes

I don't know why you have to store params like an array in a DB but an easy way to do store it first you should convert the parameters into a valid string, otherwise it can't be properly stored.

foreach($data as $tuple) {
    if (is_object($tuple)) {
        $touple = implode(",", (array)$touple);
        $connection->insert($tableName, (array)$tuple);
    }
}

another option could be to convert it as a json string

foreach($data as $tuple) {
    if (is_object($tuple)) {
        $touple = json_encode($touple);
        $connection->insert($tableName, (array)$tuple);
    }
}

And to be able to use the data when using select you should convert the data back to array using either explode() or json_encode again.Hope this woks! :)