私はlarvelでDoctrineを使用しようとしています。すべてのマッピングを行い、結果を返すことができました。しかし、問題は1対多の関係で、多くの側のArrayCollectionは空です。doctrineを使用してlaravelで空のコレクションone-to-manyを
私はProjectとClientの2つのクラスを持ち、クライアントにはProjectがたくさんあります。プロジェクトのリストではクライアントは正常に返されますが、クライアント側ではProject配列は空です。ここに私の要約Clientクラスは次のとおりです。今
<?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();
}
}
まとめProjectクラス:
<?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;
}
私はクライアントに関する情報をもたらすために、コントローラにアクセスすると、ここでのプロジェクトがあることに注意して、返されているものです空の配列:プロジェクト側で
[
{
"id": 21,
"name": "Dolores Osinski",
"responsible": "Ashton Conn",
"email": "[email protected]",
"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": {}
}
]
あなたが見ることができるように、私はクライアントを取得:
[
{
"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": "[email protected]",
"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": {}
}
}
]
何が欠けていますか?
UPDATE
マイClientRepository:
public function listProjects($id){
return response()->json($this->repository->getProjects($id));
}
それが何をリストいないとしても、このよう:特定のクライアントのためのプロジェクトを一覧表示機能で
<?php
namespace CodeProject\Repositories;
use Doctrine\ORM\EntityRepository;
class ClientRepositoryDoctrine extends EntityRepository {
public function getProjects($id) {
$client = $this->find($id);
return $client->getProjects();
}
}
マイClientController。参加フェッチと私のリポジトリでDQLを試みる
:、
<?php
namespace CodeProject\Repositories;
use Doctrine\ORM\EntityRepository;
class ClientRepositoryDoctrine extends EntityRepository {
public function getProjects($id) {
$result = $this->getEntityManager()->createQueryBuilder()->
select('c, p')->
from('CodeProject:Client', 'c')->
join('c.projects', 'p')->
where('c.id = :id')->
setParameter(':id', $id)->getQuery()->getResult();
return $result;
}
}
そして、私はこれを得たプロジェクトがロードプロジェクトを強制的に参加フェッチさえして空のままである:
[
{
"id": 1,
"name": "Mariam Jast",
"responsible": "Imogene Schowalter",
"email": "[email protected]",
"phone": "05482413294",
"address": "062 Block Parkway\nMireyabury, KS 63554-2056",
"obs": "Occaecati aperiam quibusdam maxime laboriosam aperiam sint.",
"created_at": "2017-03-20 02:29:47",
"updated_at": "2017-03-20 02:29:47",
"projects": {}
}
]
どのようにClient :: $ projectsの関係をロードしていますか? – sisve
@SimonSvensson、私はそれをそのまま1対多の双方向関係のための教義文書に従った。私がここでやったこととはまったく違うものは何も表示されません。 –