I'm looking for a way to have an associative array keys for a ODM ArrayCollection
.
The entity has the following mapping:
/**
* $fields
*
* The entities fields
*
* @ODM\ReferenceMany(targetDocument="JobboardEntity\Entity\EntityField", sort={"name"="asc"}, cascade={"all"})
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $fields;
/**
* addField
*
* Add a single field to the field collection
*
* @param EntityField $field The field to add
*/
public function addField(EntityField $field)
{
$this->fields[$field->getFieldName()] = $field;
}
Notice in the addField
method I am giving the item a index of $field->getFieldName()
.
Unfortunately, Doctrine forgets this key and returns an ArrayCollection
with numeric indexes rather than the previously set strings.
This means that in order to correctly implement $this->hasField($fieldName)
or $this->getField($fieldName)
I would need to loop over the collection and test the fieldName
value
For example
public function hasField($fieldName)
{
foreach($this->fields as $field) {
if ($fieldName === $field->getFieldName()) {
return true;
}
}
return false;
}
This, in my option, is a poor solution as I would need to load the entire collection to just check the key!
Having looked into the issue I can see that ORM has this implemented with IndexBy
mapping.
Is there any similar functionality for Doctrine ODM? What is the correct way to accomplish this?