私が開発している REST API では、Company にも Company クラスの親属性があります。
そうすれば、3 つの会社を作成できます。会社には 1 つの親会社(会社クラス) があり、複数の子会社(コレクション)を持つことができます。
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="child")
* @Expose()
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="Company", mappedBy="parent")
*/
protected $child;
public function __construct()
{
...
$this->child = new \Doctrine\Common\Collections\ArrayCollection();
}
親会社と子会社の関係をどのように作成/削除しますか?
LINK動詞について読みましたが、残念ながらすべての Web サーバーでサポートされているわけではありません。
PUT との関係を設定する必要がありますか? 親との関係を削除するにはどうすればよいですか (NULL に設定します)。
私の CompanyController は次のようになります。
/**
* Edit Action
*
* @Rest\View()
*/
public function editAction(Company $company)
{
return $this->processForm($company);
}
/**
* Remove Action
*
* @Rest\View(statusCode=204)
*/
public function removeAction(Company $company)
{
$em = $this->getDoctrine()->getManager();
$em->remove($company);
$em->flush();
}
/**
* ProcessForm Action
*/
private function processForm(Company $company)
{
$statusCode = $this->getRequest()->getMethod() == 'POST' ? Codes::HTTP_CREATED : Codes::HTTP_SEE_OTHER;
$form = $this->createForm(new CompanyType(), $company);
$form->bind($this->getRequest());
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($company);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'company_get',
array('id' => $company->getId())
),
$statusCode
);
}
return View::create($form, 400);
}
助言がありますか?