Symfony 无法从findAll()获取相关实体数据
使用findAll加载相关数据时出现问题(我尝试从相关实体加载属性用户名): 错误消息:FatalErrorException:错误:对中的非对象调用成员函数getUsername() 但我的另一个使用findOne的代码工作正常: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)
$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());