I have two entities related Orders
and Person
where one Person
can have many Orders
. These are the mapping for that entities:
class Orders {
/**
* @ORM\ManyToOne(targetEntity="Person", inversedBy="orders")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
* */
protected $person;
public function setPerson(Person $person)
{
$this->person = $person;
return $this;
}
public function getPerson()
{
$this->person;
}
}
class Person {
/**
* @ORM\Column(name="person_type", type="boolean", nullable=false)
*/
protected $person_type = 1;
/**
* @ORM\OneToMany(targetEntity="NaturalPerson", mappedBy="person")
* */
private $naturals;
/**
* @ORM\OneToMany(targetEntity="LegalPerson", mappedBy="person")
* */
private $legals;
/**
* @ORM\OneToMany(targetEntity="Orders", mappedBy="person")
* */
private $orders;
public function __construct()
{
$this->naturals = new ArrayCollection();
$this->legals = new ArrayCollection();
$this->orders = new ArrayCollection();
}
public function setPersonType($person_type)
{
$this->person_type = $person_type;
return $this;
}
public function getPersonType()
{
return $this->person_type;
}
public function getNaturals()
{
return $this->naturals;
}
public function getLegals()
{
return $this->legals;
}
public function getOrders()
{
return $this->orders;
}
}
In my controller I'm trying to get from Orders
the related record for Person
but I'm getting NULL as the JSON shows:
{
"data":[
[
"sdasdasd",
null
],
[
"werwerwer",
null
],
[
"sdfsdfsf435435",
null
]
]
}
This is how I'm getting the data in controller:
public function getOrdersAction()
{
$response = array();
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository("FrontendBundle:Orders")->findAll();
$orders = array();
foreach ($entities as $entity)
{
$order = array();
$order[] = $entity->getNickname();
$order[] = $entity->getPerson();
$orders[] = $order;
}
$response['data'] = $orders;
return new JsonResponse($response);
}
I test values on DB tables by running this query:
SELECT ord.nickname, ord.person_id, pn.id, pn.description FROM orders ord left join person pn on pn.id = ord.person_id
And this is the result:
So records are related, then what I'm doing wrong?
Emmm... You just miss "return".
public function getPerson()
{
return $this->person;
}