Hard Fitness
Hard Fitness

Reputation: 452

PHP JSON conversion from doctrine entity with nested objects

So i have the following and can't seem to covert it to a normal JSON format, I am using JMS Serializer for APIs in the FOSRestBundle.

My line that gets sent to JMS Serializer is

return array(
            'fields' => $entities,
    );

$entities includes all the info below, what you see below is a

print_r($entities);

Output:

Array
(

    [0] => Test\ClientBundle\Entity\Fieldset Object
        (
            [fieldid:Test\ClientBundle\Entity\Fieldset:private] => 43
            [title:Test\ClientBundle\Entity\Fieldset:private] => Genre
            [formid:Test\ClientBundle\Entity\Fieldset:private] => 1
            [choicestext:Test\ClientBundle\Entity\Fieldset:private] => Array
                (
                     [0] => stdClass Object
                         (
                            [label] => Folk
                         )

                )

            [defaultval:Test\ClientBundle\Entity\Fieldset:private] => 0
        )
)

When i use the built in JMS Serializer i get this:

{"fields":[{"fieldid":43,"title":"Genre","choicestext":"Array","defaultval":"0"}]

The problem is that choicestext has no nested JSON object but just a string "Array", how can I make the code insert it so it can be read as nesteed JSON object, is there a trick with JMS Serializer?

This is the actual array that gets inserted into the object property choicestext:

Array
(
    [0] => stdClass Object
        (
            [label] => Folk
        )

 )

When printed as JSON using json_encode it comes out as:

[{"label":"Folk"}]

So that is correct, just don't see why when inserted in that entities it doesn't work?

UPDATE:

print_r(json_encode($entities));

Result:

[{},{},{},{},{},{}]

Upvotes: 2

Views: 3318

Answers (2)

spekulatius
spekulatius

Reputation: 1499

I would transform the $entities into a array structure first. This can be done by this function I wrote:

/**
 * transforms any object into a array structure
 *
 * @param Object $object
 * @return array
 */
protected function _objectToArray($object)
{
    $array = array();

    foreach ((array) $object as $key => $value) {
        // is the value a object? transform it to a array itself
        if (is_object($value)) {
            $array[$key] = $this->_objectToArray($value);
        } else {
            $array[$key] = $value;
        }
    }

    return $array;
}

and then transform it into a JSON-string. It might come out strange structured if there are multiple level with different types of elements (array, objects, etc.). But you can always use the standard PHP array functions to extract the information you need easily.

Upvotes: 0

lxg
lxg

Reputation: 13107

Considering that the choicestext field is defined as string:

@ORM\Column(name="ChoicesText", type="string", length=255, nullable=false)

The serializer uses the entity metadata to determine the data type of the field. As it thinks that the field contains a string, it simply casts the current value to a string. If you want choicestext to be a string, don't set it to an array value.

Upvotes: 1

Related Questions