I'm trying to use Doctrine with laravel and I was able to make all the mappings and return some results. But the problem is with one-to-many relation, that the many side ArrayCollection is empty.
I have two classes Project and Client, and a Client has many Project. In the project listing I do return client successfuly, but, at the Client side, the Project array is empty. Here is my summarized Client class:
<?php
namespace CodeProject\Entities\Doctrine;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="clients")
*/
class Client implements \JsonSerializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\OneToMany(targetEntity="Project", mappedBy="client")
*/
private $projects;
public function __construct() {
$this->projects = new ArrayCollection();
}
}
Now the summarized Project class:
<?php
namespace CodeProject\Entities\Doctrine;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="projects")
*/
class Project implements \JsonSerializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Client", inversedBy="projects")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
}
When I access the controller to bring information about client, here is what is being returned, note that projects is an empty array:
[
{
"id": 21,
"name": "Dolores Osinski",
"responsible": "Ashton Conn",
"email": "Crist.Mario@gmail.com",
"phone": "(936)177-7976",
"address": "80311 Rossie Drives\nLake Brandyn, KS 39255-7560",
"obs": "Aliquid harum architecto eum natus qui.",
"created_at": "2017-03-19 16:33:39",
"updated_at": "2017-03-19 16:33:39",
"projects": {}
}
]
On the Project side I get the client as you can see:
[
{
"id": 1,
"name": "tenetur",
"description": "Animi ut enim voluptas. Provident alias aut animi nemo repellendus. A dolores magni ducimus sit ex.",
"progress": "39.00",
"status": 4,
"due_date": "1972-10-26 12:56:38",
"created_at": "2017-03-19 16:33:45",
"updated_at": "2017-03-19 16:33:45",
"client": {
"id": 21,
"name": "Dolores Osinski",
"responsible": "Ashton Conn",
"email": "Crist.Mario@gmail.com",
"phone": "(936)177-7976",
"address": "80311 Rossie Drives\nLake Brandyn, KS 39255-7560",
"obs": "Aliquid harum architecto eum natus qui.",
"created_at": "2017-03-19 16:33:39",
"updated_at": "2017-03-19 16:33:39",
"projects": {}
}
}
]
What Am I missing?
via Leandro Jacques