0

How do you remove and add elements to results returned by doctrine's odm. For example,

I have the query

$fruits = $dm->createQueryBuilder('Fruits') ->field('id')->in($fruit_list) ->getQuery()->execute();

$fruits returned is an arrayand then I need to iterate through this array and filter some fruits after some analysis. The shortlisting is not possible through the query builder and I need to return the filtered results.

I wanted to ask how to remove elements from this object returned by odm.

Bhavya Agarwal
  • 107
  • 1
  • 11

2 Answers2

0

I'm not sure I fully understand you, I'll assume you're trying to eliminate some fields from the entities that were returned by the ODM.

I'd turn off the hydration and work with a simple array.

Example:

$fruits = $dm->createQueryBuilder('Fruits')
             ->field('id')
             ->in($fruit_list)
             ->hydrate(false) /* Turn off hydration */
             ->getQuery()->execute();

Now all you have to do is to use unset and remove the wanted elements from the array.

Asaf David
  • 416
  • 3
  • 7
  • Thanks but I have to deal with 2 cases - one with hydration and one without it. I have already taken care of the non hydrated case like you said. But it would be great if I can do it for the hydrated objects also. So, are there any operations like unset, remove for the hydrated objects. – Bhavya Agarwal Aug 16 '12 at 15:21
  • You can't remove fields from php objects ant it has nothing to do with doctrine... What do you do with those objects ? You can set field value to null if it helps – Asaf David Aug 16 '12 at 15:45
0

When you are iterating over $fruits and making your shortlisting decision, create an ArrayCollection and add all the objects that make the cut and return that rather than the cursor.

Jamie Sutherland
  • 2,760
  • 18
  • 19