2017-06-12 5 views
0

Comment faire un tableau de MongoId dans Symfony 3.2 Doctrine ODM sérialisé/désérialisé correctement?Symfony 3.2 ODM Doctrine array de MongoId

document

namespace Acme\StoreBundle\Document; 
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 

/** 
* @MongoDB\Document(collection="Voter") 
*/ 

class Voter 
{ 

/** 
* @MongoDB\Id 
*/ 
protected $id; 


/** 
* @MongoDB\Collection 
*/ 

protected $inlist; 


/** 
* Get id 
* 
* @return id $id 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

public function setId($id) 
{ 
    $this->id = $id; 
    return $this; 
} 


/** 
* Set inlist 
* 
* @param collection $inlist 
* @return $this 
*/ 
public function setInlist($inlist) 
{ 
    $this->inlist = $inlist; 
    return $this; 
} 

/** 
* Get inlist 
* 
* @return collection $inlist 
*/ 
public function getInlist() 
{ 
    return $this->inlist; 
} 

}

Controller:

/** 
* @Route("/voter/{id}") 
* @Method({"GET"}) 
*/ 

public function getAction($id) 
{ 
    $product = $this->get('doctrine_mongodb') 
     ->getRepository('AcmeStoreBundle:Voter') 
     ->find($id); 

    if (!$product) { 
     throw $this->createNotFoundException('No product found for id ' . $id); 
    } 

    $serializer = $this->get('serializer'); 

    $data = $serializer->serialize(
     $product, 
     'json' 
    ); 

    $response = new JsonResponse(); 
    $response->setContent($data); 
    return $response; 
} 

sérialisé Json:

{ 
"id": "593e99a8de6c84f5ecec3094", 
"inlist": [ 
    { 
     "timestamp": 1417718686, 
     "pID": 3335, 
     "inc": 9127278, 
     "$id": "5480ab9e282e26070d8b456e" 
    }, 
    { 
     "timestamp": 1417718686, 
     "pID": 3335, 
     "inc": 9127273, 
     "$id": "5480ab9e282e26070d8b4569" 
    }, 
    { 
     "timestamp": 1417718686, 
     "pID": 3335, 
     "inc": 9127272, 
     "$id": "5480ab9e282e26070d8b4568" 
    }, 
    { 
     "timestamp": 1417718686, 
     "pID": 3335, 
     "inc": 9127275, 
     "$id": "5480ab9e282e26070d8b456b" 
    }, 
    { 
     "timestamp": 1417718686, 
     "pID": 3335, 
     "inc": 9127274, 
     "$id": "5480ab9e282e26070d8b456a" 
    }, 
    { 
     "timestamp": 1411754988, 
     "pID": 2674, 
     "inc": 9127271, 
     "$id": "5425abec8f3723720a8b4567" 
    } 
] 
} 

Je veux être sérialisé à être un tableau de (chaîne) id, et désérialiser retour à un tableau de ce MongoId:

{ 
"id": "593e99a8de6c84f5ecec3094", 
"inlist": ['5425abec8f3723720a8b4567', '5480ab9e282e26070d8b456b' ...] 
} 
+0

transformer simplement 'entrées de inlist' au format de votre choix dans le contrôleur ou tout autre lieu que vous voyez en forme? – malarzm

+0

J'ai fait une solution de contournement avec getInlist/setInlist transfom - mais je ne suis pas sûr que ce soit un 'bon' moyen –

Répondre

1

Puisque vous avez mentionné « droit chemin » dans les commentaires, voici comment je le ferais:

class VoterAPIRepresentation 
{ 
    public $id; 

    public $inlist = []; 

    public function __construct(Voter $voter) 
    { 
     $this->id = (string) $voter->id; 
     foreach ($voter->inlist as $i) { 
      $this->inlist[] = (string) $i['$id']; 
     } 
    } 
} 

Au-dessus de la classe est responsable de la représentation des données dans l'API depuis entité lui-même ne devrait pas être concerné par cela. Ensuite, dans le contrôleur:

public function getAction($id) 
{ 
    $product = $this->get('doctrine_mongodb') 
     ->getRepository('AcmeStoreBundle:Voter') 
     ->find($id); 

    if (!$product) { 
     throw $this->createNotFoundException('No product found for id ' . $id); 
    } 

    $serializer = $this->get('serializer'); 

    $data = $serializer->serialize(
     new VoterAPIRepresentation($product), 
     'json' 
    ); 

    $response = new JsonResponse(); 
    $response->setContent($data); 
    return $response; 
} 

Pro de cette approche est que si vous changez l'entité que vous n'avez pas besoin d'être concernés par des critères d'évaluation et les données qu'ils reviennent car ces deux êtres ne sont pas connectés. D'un autre côté, écrire de telles classes est assez ennuyeux MAIS cela paye pour des objets et des représentations complexes.

0

poseur/voies getter a fonctionné pour moi, même avec sérialiseur/désérialiseur

/** 
* Set actions 
* 
* @param collection $actions 
* @return $this 
*/ 
public function setActions($actions) 
{ 
    $re = []; 
    foreach ($actions as $action) { 
     $action['cid'] = new \MongoId($action['cid']); 
     $re[] = $action; 
    } 

    $this->actions = $re; 
    return $this; 
} 

/** 
* Get actions 
* 
* @return collection $actions 
*/ 
public function getActions() 
{ 
    $re = []; 
    foreach ($this->actions as $action) { 
     $action['cid'] = (string)$action['cid']; 
     $re[] = $action; 
    } 

    return $re; 
}