Retrieving node class data from returned PHPCR collections in Symfony CMF

119 Views Asked by At

When following the official tutorials to set up PHPCR for use as a node manager for the symfony CMF, it recommends to get all node classes of a similar type in the following manner:

$dm = $this->get('doctrine_phpcr')->getManager();
$posts = $dm->getRepository('AcmeBasicCmsBundle:Post')->findAll();

This gives me an ArrayCollection object I am then able to pass to a twig template and iterate through to get values such as post.title and post.content accordingly. However I want to be able to serialize this object for transmission over an API. Obviously this returned object contains lots of extraneous information relating to the node manager and associated routes etc, and serializing it gives massive unwieldly arrays from which it seems impossible to extract the actual class data.

The official documentation (http://doctrine-orm.readthedocs.org/projects/doctrine-phpcr-odm/en/latest/reference/working-with-objects.html) on working with objects shows how to retrieve, modify, and persist them to the database which enables you to call getter/setter functions on your class. I could use these methods to build new objects from this collection, but to pull an array of objects from the database only to loop through them and recreate from methods before serializing them seems ludicrous.

$dm = $this->get('doctrine_phpcr')->getManager();
$posts = $dm->getRepository('FrontendCmsBundle:Post')->findAll();

$apiPosts = array();
foreach ($posts as $post) {
    $apiPosts[] = array (
       'title' => $post->getTitle(),
       'content' => $post->getContent(),
    );
}

return $api->serialize($apiPosts, 'json');

I have been through the official reference for this object and tried using all the mentioned functions and none seem apprpriate - http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common.Collections.ArrayCollection.html.

Can anyone help me to retrieve an array of only my class data from phpcr? Thanks in advance

0

There are 0 best solutions below