使用find all repository方法时如何访问查询结果中的外键。特别是Jobs_id外键,它被提取到jobs对象中。
$entities = $em->getRepository('btBundle:workHours')->findAll();
$em = $this->getDoctrine()->getManager();
using foreach ($entities as $test) {
$Id=$test->getId(); //Works fine
$jobs=$test-getJobs(); // retrieves Jobs object.
$jobs->getId(); // Error: Call to a member function getid() on a non-object
}
就像它说的,它不是一个对象,我试着把它当作一个数组,比如
$jobs[0];
$jobs['id'];
下面是$jobs 的1个实例的转储给出错误error: Cannot use object of type代理…'Entity'Jobs as array
Jobs {#709 ▼
#clients: Clients {#710 ▶}
#company: Company {#638 ▶}
#scopes: PersistentCollection {#711 ▶}
#workHours: PersistentCollection {#713 ▶}
#userDetails: PersistentCollection {#715 ▶}
-id: 101
-description: "Test Job"
-jobNumber: "7844.00"
-task: null
-currencies: "USD"
-active: 1
}
我认为你的问题是你有一些工作时间没有相关的工作。
当你迭代这些实体时,$test->getjobs()
返回这些实体的null
可能它仍然是一个集合?尝试使用get_class($jobs)?