[英]Empty collection one-to-many in laravel using doctrine
I'm trying to use Doctrine with laravel and I was able to make all the mappings and return some results. 我试图将Loctavel与Doctrine结合使用,并且能够进行所有映射并返回一些结果。 But the problem is with one-to-many relation, that the many side ArrayCollection is empty. 但是问题在于一对多关系,即多方ArrayCollection为空。
I have two classes Project and Client, and a Client has many Project. 我有两个类Project和Client,一个Client有很多Project。 In the project listing I do return client successfuly, but, at the Client side, the Project array is empty. 在项目列表中,我确实成功返回了客户端,但是在客户端,Project数组为空。 Here is my summarized Client class: 这是我总结的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();
}
}
Now the summarized Project class: 现在总结一下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;
}
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? 我想念什么?
UPDATE 更新
My ClientRepository: 我的客户资料库:
<?php
namespace CodeProject\Repositories;
use Doctrine\ORM\EntityRepository;
class ClientRepositoryDoctrine extends EntityRepository {
public function getProjects($id) {
$client = $this->find($id);
return $client->getProjects();
}
}
My ClientController at the function that lists projects for a certain client: 在列出特定客户项目的函数中,我的ClientController:
public function listProjects($id){
return response()->json($this->repository->getProjects($id));
}
Even this way it's not listing anything. 即使这样,它也没有列出任何内容。
Trying with DQL at my repository with fetch join: 在我的存储库中使用fetch join尝试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;
}
}
And I got this, projects is still empty even with fetch join to force loading projects: 我明白了,即使使用强制加入项目的fetch join,项目仍然是空的:
[
{
"id": 1,
"name": "Mariam Jast",
"responsible": "Imogene Schowalter",
"email": "wHackett@gmail.com",
"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": {}
}
]
It sounds like you're never loading the relation. 听起来您从未加载该关系。 OneToMany-relations are lazily loaded by default. 默认情况下,延迟加载OneToMany关系。
You can trigger a loading by doing anything on it that would require a database call. 您可以通过执行需要数据库调用的任何操作来触发加载。 A simple foreach would suffice in this example. 在此示例中,简单的foreach就足够了。
foreach($client->getProjects() as $project) {
// ...
}
You could also configure the relation to be eager loaded. 您还可以将关系配置为渴望加载。 Source: 21.2.26. 资料来源: 21.2.26。 @OneToMany @OneToMany
/**
* @OneToMany(..., fetch="EAGER")
**/
This can also be a per-DQL-query. 这也可以是每个DQL查询。 Source: 14.2.2. 资料来源: 14.2.2。 Joins 加入
SELECT c, c.projects FROM Client c
Regarding the loading of Project.client, that could be the entity-map in action. 关于Project.client的加载,可以是正在运行的实体映射。 You've already loaded the client from the entity manager, so when a Project instance says "I have client_id = 1" your entity manager notes that Client #1 is already loaded, and associates that with the Project.client relation. 您已经从实体管理器加载了客户端,因此当Project实例显示“我有client_id = 1”时,您的实体管理器会注意到已经加载了Client#1,并将其与Project.client关系相关联。
The other way isn't possible since there is no way for Doctrine to know that you've loaded all possible projects that a client can have. 另一种方法是不可能的,因为Doctrine无法知道您已经加载了客户端可以拥有的所有可能项目。 The only true way to find out is to query the database. 唯一真正的发现方法是查询数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.