Doctrine と FOS Rest Bundle (JMS シリアライザーを使用) で Symfony2 を使用しています。FatherとChildの 2 つのエンティティ があります。
<?php
namespace Acme\Bundle\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Father
*
* @ORM\Table()
* @ORM\Entity
*/
class Father {
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
**/
private $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
}
と
namespace Acme\Bundle\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Child
*
* @ORM\Table()
* @ORM\Entity
*/
class Child {
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
**/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Acme\Bundle\CoreBundle\Entity\Father")
**/
private $father;
}
ルートがあります:
acme_test_child_all:
defaults: { _controller: AcmeCoreBundle:Test:childAll }
path: /child/
acme_test_father_get:
defaults: { _controller: AcmeCoreBundle:Test:fatherGet }
path: /father/{id}
そして最後に、これらのルートに対するアクションを備えたコントローラーがあります。
<?php
namespace Acme\Bundle\CoreBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestController extends Controller {
/**
* @Rest\View()
*/
public function childAllAction() {
$children = $this->getDoctrine()
->getRepository('AcmeCoreBundle:Child')
->findAll();
return $children;
}
/**
* @Rest\View()
*/
public function fatherGetAction($id) {
$father = $this->getDoctrine()
->getRepository('AcmeCoreBundle:Child')
->findById($id);
return $father;
}
}
GET /child/を呼び出すと、期待どおりの応答が得られます。
[
{
"id": 1,
"father": {
"id":1,
"name":"Father"
}
}
]
ネストされた応答の代わりに、Father リソースのuriを取得したいと思います。
[
{
"id": 1,
"father": "/father/1"
}
]
これを達成するための最良の方法は何ですか?