2015-06-22 51 views
0

如何禁用教条2中的延迟加载?禁用懒加载教条zend框架?

$em = $this->getEntityManager(); 
$repo = $em->getRepository('Application\Entity\StudentClass'); 
$result = $repo->findBy(array('pkStudentClass' => '1')); 

print_r($result); 

我来这里太多的数据和脚本失败。

+1

您将始终从原则中获取对象。 '$ result'将是一个'Doctrine \ ORM \ PersistentCollection'(它负责延迟加载)。使用'$ result-> toArray()'返回集合中所有'Application \ Entity \ StudentClass'实体的'array'。 – AlexP

+0

这是真的,谢谢。 – user3911183

回答

1

请使用以下查询进行数据检索 您可以在此检索需要的列并检索所需的记录。

$query = $this->getEntityManager()->createQueryBuilder() 
        ->select('U.id,U.name') 
        ->from('Application\Entity\StudentClass', 'U') 
        ->where('U.pkStudentClass = :pkStudentClass') 
        ->setParameter('pkStudentClass', 1) 
        ->setMaxResults(20); 
        ->orderBy('id', 'DESC') 
        ->getQuery(); 

$ result = $ query-> getScalarResult();

+0

我知道,但有时我们需要检索整个对象。 – user3911183