G
G
grabbee2017-04-22 12:45:35
symfony
grabbee, 2017-04-22 12:45:35

How to reduce json nesting from JMS serializer?

I can't figure out what to do with connections. OneToOne - in the first entity (user) there is a tags field, which is connected to another table (tag_string) in which the list of tags is cached as a comma-separated string. At the output, I wanted to get user data with a tag line field from the second table.
But as I understand it, there is a nesting of the type

{
    name: 'Ivan',
    age: 20,
    tags: {
        str: "tag1, tag2, tag3 ..."
    }
}

How to get rid of nesting and get tags right away: "tag1, tag2, tag3
...
"
/**
     * @View()
     * @return array
     */
    public function getAction(User $user)
    {
        return $user;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lexey Felde, 2017-04-23
@grabbee

You can try using virtual methods (VirtualProperty):

class User {
...
    /**
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("tags")
     */
    public function getTags()
    {
        return explode(',', $this->tags->getStr());
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question