2

I have a problem with Doctrine2 find methods. They all return as array instead of ArrayCollection.

Is there any away to force them to return a ArrayCollection? I remember this also happened with a custom repository query. I had to do: new ArrayCollection($result).

3
  • ArrayCollection is meant to support OneToMany or ManyToMany collections of relations, but find* methods just returns some results, no need for a Collection here. Commented Aug 12, 2013 at 15:31
  • OK. But it would be nice to do $results->count() instead of count($results) or having any of the methods of ArrayCollection instead of a flat array Commented Aug 12, 2013 at 15:35
  • 1
    PHP is not Java so arrays are still "poor native" types. ArrayCollection are object oriented arrays, but do not add some new features. It just provides an object interface to map collections in entities, not to break the object structure when handling relations. Commented Aug 13, 2013 at 7:31

1 Answer 1

1

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;  
Sign up to request clarification or add additional context in comments.

1 Comment

It works but it is too slow. Did you discover anything else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.