简体   繁体   English

doctrine2查找方法返回数组而不是ArrayCollection

[英]doctrine2 find methods returns array instead of ArrayCollection

I have a problem with Doctrine2 find methods. 我对Doctrine2查找方法有疑问。 They all return as array instead of ArrayCollection. 它们都以数组而不是ArrayCollection的形式返回。

Is there any away to force them to return a ArrayCollection? 有什么强迫他们返回ArrayCollection的方法吗? I remember this also happened with a custom repository query. 我记得自定义存储库查询也发生了这种情况。 I had to do: new ArrayCollection($result). 我必须做:new ArrayCollection($ result)。

Here is my dirty, low-tech approach. 这是我肮脏,技术含量低的方法。

// your findBy* here:
$entities = $em->getRepository($entclass)->findBy($entFilter, array('id' => 'DESC'));

// my one-liner conversion here:
$entitiesCollection= new \Doctrine\Common\Collections\ArrayCollection($entities);

Then I confirmed I could use those handy ArrayCollection methods like last(), count(), contains(), etc. 然后我确认我可以使用那些方便的ArrayCollection方法,例如last(),count(),contains()等。

var_dump($entitiesCollection->count(), $entitiesCollection); print \strftime('%c') . __FILE__ . __LINE__ . __FUNCTION__; die;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM