Assuming I have two entities:
- entity A: locations
- entity B: images
one location owns several images ( one-to-many ). The images might have a property type. I often come to a use case, where i need to filter images by type. At the moment I do it through lifecycle callbacks like you'll see in the following example:
/**
* This functions sets all images when the entity is loaded
*
* @ORM\PostLoad
*/
public function onPostLoadSetImages($eventArgs)
{
$this->recommendedForIcons = new ArrayCollection();
$this->whatYouGetImages = new ArrayCollection();
foreach ($this->getImages() as $image) {
if ('background-image' === $image->getType()->getSlug()) {
$this->backgroundImage = $image;
} elseif ('product-icon' === $image->getType()->getSlug()) {
$this->mainIcon = $image;
} elseif ('product-image' === $image->getType()->getSlug()) {
$this->productImage = $image;
} elseif ('recommended-icon' === $image->getType()->getSlug()) {
$this->recommendedForIcons->add($image);
} elseif ('what-you-get-image' === $image->getType()->getSlug()) {
$this->whatYouGetImages->add($image);
} elseif ('productshoot-fullcolour' === $image->getType()->getSlug()) {
$this->productImageFullColor = $image;
} elseif ('product-overview' === $image->getType()->getSlug()) {
$this->productOverviewImage = $image;
}
}
}
I am wondering if it is possible to search for an element in a doctrine array collection and not for the key or the element itself only.
Thank you.