2

When I try to get data from multiple collections code it is giving me data only from the first collection i.e collections are project_0, project_1, project_2, project_3

for($i = 0; $i <= 3; $i++){
   $dm->getClassMetadata('\Application\Document\Product')->setCollection('product_'. $i);
   $record = $dm->getRepository('\Application\Document\Product')->findOneBy($condition);
   print_r($record);

}

I tried to clear flush but noting is working. Please let me know the right way to do it?

Navneet Garg
  • 1,364
  • 12
  • 29

1 Answers1

0

Executing the code above will save the information only from the last collection in $record as you are overwriting the data in it with every iteration.

To fix it, you can create an array, let's say $records = array(); and then in each iteration you can do something like this:

array_push($records, $dm->getRepository('\Application\Document\Product')->findOneBy($condition));

After you're done, you'll have all the data in $records. I hope that helps.

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
  • no this is not the case i already print $record in loop but it is not working – Navneet Garg May 25 '15 at 14:08
  • There is no print statement in the code above so please provide the full version of the code that you are using and eventually further explanations describing your goal? – mmvsbg May 25 '15 at 14:09
  • Ok, there is a print statement now. I would suggest that the problem comes from the values of '$condition' as the value for it does not change anywhere but you'll need to provide more code.... – mmvsbg May 25 '15 at 14:11
  • condition is same for all collections only collection name is changing – Navneet Garg May 25 '15 at 14:13
  • The line that you have will always return the same value if '$condition' is not changing. The line that you have above it is also a 'get' statement which means that you are not changing anything as far as I understand.... – mmvsbg May 25 '15 at 14:17
  • change collection name ->setCollection('product_'. $i); – Navneet Garg May 25 '15 at 14:21
  • In that case probably one of those two functions that you're calling is at fault but we'll need to see more code to be sure – mmvsbg May 25 '15 at 14:24
  • this is the only code i want to get data from multiple collection. when i tried this code as single attempt it is working but when i use loop it is returning only first collection data. i thing odm store data in cache. but flush or clear method is not working – Navneet Garg Jul 17 '15 at 05:31