Skip to content

Instantly share code, notes, and snippets.

@baldurrensch
Created October 26, 2012 20:28
Show Gist options
  • Select an option

  • Save baldurrensch/3961294 to your computer and use it in GitHub Desktop.

Select an option

Save baldurrensch/3961294 to your computer and use it in GitHub Desktop.
Problem getting relations working with HateoasBundle
<?php
// This is just copy pasted in here.
class Controller {
public function getMemberAction($memberId)
{
$country = $this->getDoctrine()->getRepository('HautelookApiBundle:Countries')->find('US');
$serializedCountry = $this->get('serializer')->serialize($country, 'json');
$response = new Response($serializedCountry);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
<?php
namespace Hautelook\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\SerializerBundle\Annotation as JMSSerializer;
use FSC\HateoasBundle\Annotation as Hateoas;
/**
* Hautelook\ApiBundle\Entity\Countries
* @Hateoas\Relation("self", route = "hautelook_api_inspiration_getinspired")
* @ORM\Table(name="countries")
* @ORM\Entity
*/
class Countries implements HalResource
{
/**
* @var string $countryIso
*
* @ORM\Column(name="country_iso", type="string", length=2, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $countryIso;
/**
* @var string $countryIso3
*
* @ORM\Column(name="country_iso3", type="string", length=3, nullable=false)
*/
private $countryIso3;
/**
* @var string $countryName
*
* @ORM\Column(name="country_name", type="string", length=80, nullable=false)
*/
private $countryName;
/**
* @var Quads
* @JMSSerializer\Type("array")
* @ORM\ManyToMany(targetEntity="Quads", inversedBy="countryIso")
* @ORM\JoinTable(name="shipping_country_quad_exclusions",
* joinColumns={
* @ORM\JoinColumn(name="country_iso", referencedColumnName="country_iso")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="quad_id", referencedColumnName="quad_id")
* }
* )
*/
private $quad;
public function __construct()
{
$this->quad = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get countryIso
*
* @return string
*/
public function getCountryIso()
{
return $this->countryIso;
}
/**
* Set countryIso3
*
* @param string $countryIso3
*/
public function setCountryIso3($countryIso3)
{
$this->countryIso3 = $countryIso3;
}
/**
* Get countryIso3
*
* @return string
*/
public function getCountryIso3()
{
return $this->countryIso3;
}
/**
* Set countryName
*
* @param string $countryName
*/
public function setCountryName($countryName)
{
$this->countryName = $countryName;
}
/**
* Get countryName
*
* @return string
*/
public function getCountryName()
{
return $this->countryName;
}
/**
* Add quad
*
* @param Hautelook\ApiBundle\Entity\Quads $quad
*/
public function addQuads(\Hautelook\ApiBundle\Entity\Quads $quad)
{
$this->quad[] = $quad;
}
/**
* Get quad
*
* @return Doctrine\Common\Collections\Collection
*/
public function getQuad()
{
return $this->quad;
}
}
{
"country_iso": "US",
"country_iso3": "USA",
"country_name": "United States",
"quad": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment