Doctrine replace associations with their ids

171 views Asked by At

I'm building an API using FOSRestBundle in Symfony2 and I'm trying to remove the entity associations from an entity with no luck, to minimize the JSON data sent in the API response.

public function helperRemoveAssociations($entities) {

    $em = $this->getEntityManager();

    $data = array();

    /** @var WBEntity $entity */
    foreach ($entities as $entity) {

        $entityAssociations = $entity->getAssociationNames($em); // ex: array('category', 'comments')

        foreach ($entityAssociations as $associationName) {

            $associationGetter = 'get' . ucfirst($associationName);
            $associationSetter = 'set' . ucfirst($associationName);

            /** @var WBEntity $associationObject */
            $associationObject = $entity->$associationGetter();
            $associationId = $associationObject->getEntityId($em); // ex: 51

            $entity->$associationSetter($associationId); // I am not allowed to write an integer 
        }

        $data[] = $entity;
    }

    return $data;
}

How can I accomplish this, replacing the associated entities with their respective ids.

0

There are 0 answers