0

I am using Doctrine in my PHP app to return a result set using the following code

 $dm = $this->get('doctrine.odm.mongodb.document_manager');

        $query = $dm->createQueryBuilder('SomeBundle:Listing')
        ->select('title')
        ->field('userId')->equals(1);

        $listings = $query->getQuery()->execute();
        $listings_array = $listings->toArray(); <--- WHY NOT RETURNING AN ARRAY?????


            $data = array('success'=>true,'listings' => $listings_array, 'displaymessage' => $classifieds->count(). " Listings Found");

What gets out out is the following:

{"success":true,"listings":{"50831582253b4acf09000000":{"id":"50831582253b4acf09000000","title":"fddfds","assets":[],"discussions":[]}},"displaymessage":"1 Listings Found"}

I am wanting an array and not a dictionary.

Any help?

4

1 回答 1

1

I havent messed with the ODM much but i suspect Doctrine always uses the key for the record as the key in the array when calling toArray on a collection, it makes it easier for most of the cases when you would want to do this, especially since there is no distinction in php between a dict/hash and an array.

Call array_values on it if you want a numerically indexed array.

$data = array(
  'success'=>true,
  'listings' => array_values($listings_array), 
  'displaymessage' => $classifieds->count(). " Listings Found"
);
于 2012-10-22T15:00:45.543 回答