2

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:

[{},{},{},{},{},{}]
8
  • What happens if you just json_encode() this data structure? Commented Aug 22, 2014 at 18:26
  • @MikeBrant just posted the output you wanted, i am suspecting it might have to do with doctrine having certain properties in the actual entity such as choicestext as a string from the database Commented Aug 22, 2014 at 19:18
  • json_encode cannot access the properties of the entity, because they are private. Therefore you get an array with six empty JSON objects. Commented Aug 22, 2014 at 19:37
  • @lxg correct, i would have to create a separate object basically copying the content of the entity object. Commented Aug 22, 2014 at 19:41
  • @HardFitness: Or, you could work with the entity metadata. Commented Aug 22, 2014 at 21:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

i think i have to stick to using JMS Serializer bundle to modify my data to get accepted in the API. I want choicestext to be an array but it can't be as the property is a string.
Can't you set the column type to array?
No, the type is based on what the sql database has. So choicestext is a string ChoicesText varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
Uhm, Doctrine's array type is also a string in the database. Doctrine handles serialization/unserialization on the fly. So, simply define the field as @ORM\Column(type="array"), and you're good. (By the way, when using Doctrine, it doesn't matter what the DB supports. If Doctrine supports it, you don't have to worry. Unless you're using native queries through Doctrine, of course.)
I tried that but gave me a string as well. foreach($entities as $entity) { if($entity->gettypeof() == "checkbox") { $entity->setchoicestext($choices_array); } } ended up showing "Array" for choicestext
|
0

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.

2 Comments

tried that but it came out real weird. Just need to get into the $entities the array as a real array not as a string.
Yeah, it might come out strange structured if there are multiple level with different types of elements (array, objects, etc.). You can always use the standard PHP functions to extract the information you'd like to have easily from the array. If the result is too strange I'd probably build a adapter to transform it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.