0

I have an array from a database in Symfony Doctrine that looks like this (using dd()):

array:2 [
  0 => App\Entity\Bg {#518
    -id: 2
    -name: "company"
    -partner: "Bill"
    -mail: "mail"
    -status: "awaiting"
    -last_changed: "25.03.2020"
  }
  1 => App\Entity\Bg {#520
    -id: 3
    -name: "company 2"
    -partner: "Max"
    -mail: "mail"
    -status: "awaiting"
    -last_changed: "25.03.2020"
  }
]

after using

json_encode($bgs);

the result is only

"[{},{}]"

What exactly is going on behind the scenes? Obviously I want json_encode to transform my array into a json_array, so I can send it in a response.

  • They look like objects of type `App\Entity\Bg` which may not have public properties. – Nigel Ren Mar 25 '20 at 13:17
  • have a look at this question https://stackoverflow.com/questions/6706485/how-to-encode-doctrine-entities-to-json-in-symfony-2-0-ajax-application – Umer Abbas Mar 25 '20 at 13:20
  • If you're using PHP 7.4, you can utilize [custom object serialization](https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.standard.magic-serialize). If you're using earlier versions, you could implement the [serializable](https://www.php.net/manual/en/class.serializable) interface on your entity. Note that this has been deprecated as of 7.4. – El_Vanja Mar 25 '20 at 13:31

0 Answers0