如何在Symfony 3.2中制作MongoId数组?Doctrine ODM正确序列化/反序列化?
文档
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 :(得分: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;
}
这种方法的专业是,如果你改变实体,你不需要关心他们返回的任何端点和数据,因为这两个生命没有连接。另一方面,编写这样的类非常无聊,但它为复杂的对象和表示带来了回报。
答案 1 :(得分:0)
setter / getter方式对我来说即使使用序列化器/解串器
也是如此/**
* 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;
}