Mongodb ArrayCollection中的命名集合键
我正在寻找一种为ODMMongodb ArrayCollection中的命名集合键,mongodb,doctrine-orm,doctrine-odm,arraycollection,Mongodb,Doctrine Orm,Doctrine Odm,Arraycollection,我正在寻找一种为ODMArrayCollection创建关联数组键的方法 实体具有以下映射: /** * $fields * * The entities fields * * @ODM\ReferenceMany(targetDocument="JobboardEntity\Entity\EntityField", sort={"name"="asc"}, cascade={"all"}) * @var \Doctrine\Common\Collections\ArrayColle
ArrayCollection
创建关联数组键的方法
实体具有以下映射:
/**
* $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;
}
请注意,在addField
方法中,我给了该项一个索引$field->getFieldName()
不幸的是,Doctrine忘记了这个键,并返回一个带有数字索引的ArrayCollection
,而不是以前设置的字符串
这意味着为了正确地实现$This->hasField($fieldName)
或$This->getField($fieldName)
我需要在集合上循环并测试fieldName
值
比如说
public function hasField($fieldName)
{
foreach($this->fields as $field) {
if ($fieldName === $field->getFieldName()) {
return true;
}
}
return false;
}
在我看来,这是一个糟糕的解决方案,因为我需要加载整个集合来检查密钥
我可以看出,ORM已经
ODM是否有类似的功能?实现这一点的正确方法是什么?请查看
如果使用@ODM\ReferenceMany(strategy=“set”)(也适用于EmbedMany),则集合将存储为BSON对象,并在加载时设置相应的键:
我不能完全确定DB本身对性能的影响。我想将集合存储为BSON对象会稍微慢一点,但正如您所说的,加载整个集合(以及您要检查的实体)并不是一件好事,它有助于保持代码更干净一点