Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 无法从findAll()获取相关实体数据_Symfony_Select_Doctrine_Find_Relational Database - Fatal编程技术网

Symfony 无法从findAll()获取相关实体数据

Symfony 无法从findAll()获取相关实体数据,symfony,select,doctrine,find,relational-database,Symfony,Select,Doctrine,Find,Relational Database,使用findAll加载相关数据时出现问题(我尝试从相关实体加载属性用户名): 错误消息:FatalErrorException:错误:对中的非对象调用成员函数getUsername() 但我的另一个使用findOne的代码工作正常: $repository = $this->getDoctrine()->getRepository('MyAwesomeBundle:Employee'); $employee = $repository->findOneByCode($code)

使用findAll加载相关数据时出现问题(我尝试从相关实体加载属性用户名):

错误消息:FatalErrorException:错误:对中的非对象调用成员函数getUsername()

但我的另一个使用findOne的代码工作正常:

$repository = $this->getDoctrine()->getRepository('MyAwesomeBundle:Employee');
$employee = $repository->findOneByCode($code);
die("".$employee->getUser()->getUsername());

我的问题是,如果我使用findAll加载所有数据,如何在foreach中加载$employee->getUser()->getUsername()?

数据库中有一名
员工,但该员工没有与其关联的
用户

当只加载一个时,您加载的是一个与用户关联的用户,但当循环所有用户时,您偶然发现一个没有关联的用户,此时,您的代码失败

解决方案:找到没有关联的
用户的
员工
,并将其删除,或将
用户
与其关联

$repository = $this->getDoctrine()->getRepository('MyAwesomeBundle:Employee');
$employee = $repository->findOneByCode($code);
die("".$employee->getUser()->getUsername());