Doctrine 2 findBy *返回对象

Is it possible to change findBy* method to return an object (my custom) with entities instead of an array?

You're probably better served writing a custom function in your entity repository.

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.

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

Use findOneBy instead of findBy. findOneBy is return as an object.

http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

$obj = $this
    ->getDoctrine()
    ->getRepository("VendorXXXBundle:EntityName")
    ->findOneBy(array $criteria)