2017-06-12 15 views
0

MongoIdの配列をSymfonyに作る方法3.2 Doctrine ODMは正しく直列化/直列化解除されていますか?Symfony 3.2 ODM Doctrine配列の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

と私のために働きましたか? – 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

セッター/ゲッターの方法は、ただ、コントローラまたはあなたが合う他の場所に、ご希望の形式に `inlist`のエントリを変換しても、シリアライザ/デシリアライザ

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