In a REST API that I'm developing a Company has a parent attribute which is also of the Company Class.
That way I can create a three of Companies. A Company has one parent Company (Company Class) and can have multiple children Companies (Collection)
/**
* @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();
}
How would I go about and make/remove that relationship of parents and children companies?
I've read about the LINK verb but I'm afraid it's not supported by all webservers.
Should I set the relationship with PUT? How would I then remove the relationship to the parent (set it to NULL).
My CompanyController looks like this:
/**
* 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);
}
Any suggestions?