3

How can I convert an Array into JSON in Doctrine ?

So far I have tried this.Here is my code:

require_once ("../Users.php");
require_once("../../test/doctrine/cli-config.php");
require_once "../../test/doctrine/bootstrap.php";

            $user_list = array();

            $usersRepository = $entityManager->getRepository('Users');

            $users = $usersRepository->findAll();  

            echo "<pre>";
            print_r($users);

            foreach ($users as $user) 
            {
                $user_list[] = array('user_list'=>$user);   
            }


  json_encode($user_list)

The print_r() section is returning me this:

Array
(
    [0] => Users Object
        (
            [id:Users:private] => 1
            [lastName:Users:private] => User1
            [firstName:Users:private] => Test1
            [city:Users:private] => ABC
            [country:Users:private] => XYZ
            [email:Users:private] => user1@yahoo.com
        )

    [1] => Users Object
        (
            [id:Users:private] => 2
            [lastName:Users:private] => User2
            [firstName:Users:private] => Test1
            [city:Users:private] => ABC
            [country:Users:private] => XYZ
            [email:Users:private] => user2@yahoo.com
        )

)
[{"user_list":{}},{"user_list":{}}]

See the Json Response is Empty. Can anyone help me with that?

Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92
  • 2
    I recommand using [**JMS/Serializer**](https://github.com/schmittjoh/serializer) to export your entities – Touki Oct 23 '13 at 07:33
  • @Touki I am getting these errors Undefined variable: serializer Fatal error: Call to a member function serialize() on a non-object – Hassan Sardar Oct 23 '13 at 07:42
  • Please, take time to [read the documentation](http://jmsyst.com/libs/serializer) correctly, and to [search for your error](http://stackoverflow.com/a/12769983/1607098) before asking. I'm pretty sure you didn't follow the installations steps which is why you are getting this error. – Touki Oct 23 '13 at 07:48
  • @Touki I did the same as they explained in Documention, but its still giving me these errors. – Hassan Sardar Oct 23 '13 at 08:07
  • This is basically the lbrary is not installed properly – Hassan Sardar Oct 23 '13 at 08:08

1 Answers1

4

You do not have public properties in your entities... that's why you get an empty json.

I am using for this purpose EntitySerializer

Usage for your case should be:

$eSerializer = new Bgy\Doctrine\EntitySerializer($entityManager);
$result = $eSerializer->toArray($users);

but this is just a personal preference. You could an should use the standard Serializer class of Symfony framework

Udan
  • 5,429
  • 2
  • 28
  • 34
  • Do i have to include this library somewhere? – Hassan Sardar Oct 23 '13 at 06:55
  • Plus for some reason I could not use $em ,it gives me error, I use $entityManager – Hassan Sardar Oct 23 '13 at 07:30
  • I have installed that library but its giving me error Warning: get_class() expects parameter 1 to be object, array given in C:\xampp\htdocs\test\doctrine\vendor\doctrine\common\library\Bgy\Doctrine\EntitySerializer.php on line 69 – Hassan Sardar Oct 23 '13 at 09:31
  • You must use `foreach ($users as $user) {$user_list[] = $eSerializer->toArray($user); }` and after that json_encode($user_list). – Udan Oct 23 '13 at 09:36
  • require_once "../../test/doctrine/vendor/doctrine/common/library/Bgy/Doctrine/EntitySerializer.php"; $user_list = array(); $usersRepository = $entityManager->getRepository('Users'); $users = $usersRepository->findAll(); $eSerializer = new Bgy\Doctrine\EntitySerializer($entityManager); foreach ($users as $user) { $user_list[] = $eSerializer->toArray($user); } $result = $eSerializer->toArray($users); echo json_encode($result); – Hassan Sardar Oct 23 '13 at 09:49
  • And I m Getting a warning : Warning: get_class() expects parameter 1 to be object, array given in C:\xampp\htdocs\test\doctrine\vendor\doctrine\common\library\Bgy\Doctrine\EntitySerializer.php on line 69 – Hassan Sardar Oct 23 '13 at 09:50
  • require_once in Symfony2? remove $result = $eSerializer->toArray($users); and then echo json_encode($user_list); – Udan Oct 23 '13 at 09:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39812/discussion-between-code-hunter-and-udan) – Hassan Sardar Oct 23 '13 at 10:11
  • It worked Thanks alot for the help :) Plus also tell me what to use instead of require_once in Doctrine2 ??? – Hassan Sardar Oct 23 '13 at 10:25