eidsonator
eidsonator

Reputation: 1325

FOSRestBundle & Propel & JMSserializerBundle Symfony2

I'm trying to follow William Durand's tutorial here to create a rest api using fosrest bundle and propel. I've been beating my head against the serialization for 2 days now. I've found some users that ran into a similar problem, but have found no solution.

Here is my controller:

/**
 * @Rest\View
 */
public function allAction(){
    $messages = MessageQuery::create()->find();
    return array('messages' => $messages);
}

I'm expecting an output of messages and ids, however I get an output with information about my model:

{ "messages" : { "formatter" : { "as_columns" : [  ],
      "class" : "My\\FooBundle\\Model\\Message",
      "collection_name" : "PropelObjectCollection",
      "current_objects" : [  ],
      "db_name" : "fooDB",
      "has_limit" : false,
      "peer" : "My\\FooBundle\\Model\\MessagePeer",
      "with" : [  ]
    },
  "model" : "My\\FooBundle\\Model\\Message"
} }

I've ensured that my jmsserializer bundle has the propelcollectionhandler.php patch.

I have this in my app/config/config.yml

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            Propel:
                namespace_prefix: "My\\FooBundle\\Model\\om"
                path: "@MyFooBundle/Resources/config/serializer"

I've seen that the namespace_prefix is blank on some of the examples on Github, because they claim the BaseModel in propel has no namespace, but my autogenerated propel base models have a namespace, is this something new in 1.7? I have tried it with and without a namespace_prefix and I do have a Model.om.BaseTableMessage.yml file in the specified directory.

Has anybody ran into this problem? How did you solve it? Thanks!

Upvotes: 1

Views: 802

Answers (1)

oujesky
oujesky

Reputation: 2856

Solution from Quentin Favrie from https://groups.google.com/d/msg/symfony2/FqiqJ2dqAM8/8b-9xucG7k4J worked for me.

Add this code to My/FooBundle/Resources/config/services.yml

parameters:
    jms_serializer.propel_collection_handler.class: JMS\Serializer\Handler\PropelCollectionHandler

services:
    jms_serializer.propel_collection_handler:
        class: %jms_serializer.propel_collection_handler.class%
        tags:
            - { name: jms_serializer.subscribing_handler }

Upvotes: 4

Related Questions