Can anybody show example of using forAll
method of Doctrine\Common\Collections\ArrayCollection?
It's pretty straightforward. The class you linked to implements the forAll
method in a following manner:
foreach ($this->_elements as $key => $element) {
if ( ! $p($key, $element)) {
return false;
}
}
So, based on that you should invoke the forAll
like:
$collection = ... #some data
$collection->forAll(function($key, $item){
// Your logic here, based on $key and $item
});
Hope this help....
Student
, which has a OneToMany
to student's marks.You want to check if student has passed all the subjects he/she elected
$student = ....
$allPassed = $student->getMarks()->forAll(function($key, $mark){
return $mark->getValue() != 'F';
});
The $allPassed
will hold TRUE
if all marks were either 'A', 'B', 'C' or 'D'. Even if one of them were F
if will be FALSE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With