2015-06-22 92 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