I have an entity which stores an array of the same entity, this is called Review[], it stores:
ID, review
Now within my controller, I loop through an array of ratings which stores:
ID, reviewID
How do I grab the the Review object from the Review array using the reviewID from the ratings?
This is what I've done so far:
foreach($ratings as $rating)
{
// Grab the review from the id stored in rating
$review = $reviews->findById ??
}
In PHP, you can use a foreach loop as you've done, but you can also use a foreach loop as such:
foreach($ratings as $id->$rating) {
//...
}
Hope that answers your question!
I see the point that @Nietvoordekat is trying to make.
Try this:
foreach($ratings as $key => $value){
if ($key == 'reviewID'){
$repo = $this->getDoctrine()->getRepository('AppBundle:Review');
$review = $repo->findOneBy( array( 'id' => $value ) );
}
}
For the above, I'm not sure if you Entity is called Review
or if the Review entity Id
is called id
. So you may need to change that. But I think this should help you.