2017-06-12 75 views
0

如何在Symfony 3.2中創建MongoId的數組原理ODM序列化/反序列化正確?Symfony 3.2 ODM MongoId的原理陣列

文獻

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; 
} 

}

控制器:

/** 
* @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; 
} 

序列化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" 
    } 
] 
} 

我希望它被序列是陣列(串) ID的,和反序列化回MongoId的數組:

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

只是變換'inlist'的條目,以你想要的格式在控制器或您認爲合適的任何其他地方? – malarzm

+0

我對getInlist/setInlist transfom做了一個解決方法 - 但不確定它是一種「正確」方式 –

回答

1

既然你在評論中提到的「正確的方式」這裏是我會怎麼做:

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']; 
     } 
    } 
} 

以上級負責,因爲實體API數據表示本身不應該擔心這一點。然後,在控制器:

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是如果你改變了實體,你不需要任何端點和數據他們回來,因爲這兩個生命沒有連接有關。另一方面,編寫這樣的類很乏味,但它爲複雜的對象和表示提供了回報。

0

的setter /吸氣方式工作對我來說,即使串行器/解串器

/** 
* 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; 
}