cc96ai
cc96ai

Reputation: 751

how to convert Doctrine object into json

I am using Doctrine 1.2, how could I get the query object into json / array format?

$user = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id')
->execute();

Upvotes: 14

Views: 35898

Answers (5)

ScorpioT1000
ScorpioT1000

Reputation: 358

Now Doctrine ORM Transformations is out, it allows to convert entities to scalar arrays and back

class User implements ITransformable {
    use Transformable;
// ...
echo json_encode($user->toArray());

Upvotes: -1

Tadas Sasnauskas
Tadas Sasnauskas

Reputation: 2313

Trouble with $record->exportTo('json') is that it will export all record fields. And in most cases it's not a desirable behaviour (for e.g. when this piece of json should be passed to browser). One way to limit the scope of export is to specify fields in DQL select:

$user = Doctrine_Query::create()
            ->select('u.id, u.name')
            ->from('User u')
            ->addWhere('u.id = ?', $id)
            ->fetchOne();

$user_json = $user->exportTo('json');

$user_json then will have something like this:

{
    "id": 123,
    "name": "John Smith",
    "password": null,
    "deleted": null
}

So it does not expose "password" field value but does expose underlying database structure. Again, might not be what we want. What I do is specify fields in DQL select + fetch as array then json encode:

$user = Doctrine_Query::create()
            ->select('u.id, u.name')
            ->from('User u')
            ->addWhere('u.id = ?', $id)
            ->fetchOne(array(), Doctrine::HYDRATE_ARRAY);

$user_json = json_encode($user);

In this case json will look like something like:

{
  "id": 123,
  "name": "John Smith"
}

Upvotes: 8

inalgnu
inalgnu

Reputation: 21

For JSON :

$user->exportTo('json');

;-)

Upvotes: 1

cc96ai
cc96ai

Reputation: 751

$users2 = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id');
$tmp2 = $users2->fetchArray();

I don't know why the toArray() will give the other field in the table, e.g. it will have the "password" field, it seems fetchArray() can give me the correct fields in query.

toArray()

Array
(
    [0] => Array
        (
            [id] => 1
            [username] => user1
            [password] => password
            [firstname] => John
            [lastname] => Smith
        )

fetchArray()

Array
(
    [0] => Array
        (
            [id] => 1
            [username] => user1
            [firstname] => John
            [lastname] => Smith
        )

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

A solution might be to use the toArray() method on the $user object, to have a simple array containing only the data youre interested in, and, then, use json_encode to convert that PHP array to a JSON string.

Something like this, I suppose :

$user = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id')
->execute();

$userArray = $user->toArray();
$json = json_encode($userArray);

(Not tested, but it should not be too far from working...)

Upvotes: 31

Related Questions