Closed davidemerlitti closed 2 years ago
Have you tried passing actual documents (or just proxies obtained with DocumentManager::getReference
) to the refB
array? Given ODM tries to prepare values and gets to reading identifiers, it could work. If it does not please try asking on StackOverflow under doctrine-odm
tag, you'll have better chances to have your question answered :)
Dear @malarzm
thank you for you suggestion. I tried to pass both documents returned from DocumentManager::find () and proxies returned from DocumentManager::getReference (). I get the same wrong result.
Hi @malarzm,
the only way to fix the problem was to patch the function DocumentPersister::prepareQueryElement(...) at line 1222 in this way:
if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
//return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
if (is_array($value)) {
$ids = [];
foreach($value as $item) {
$ids[] = $targetClass->getDatabaseIdentifierValue($item);
}
return [[$fieldName, $ids]];
} else {
return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]];
}
}
Support Question
I have this class (a semplified version of my real class...):
I want to update the
A
object identified by $oid withhasManyB
property set to an array of two objects of classB
identified by $oid1 and $oid2:The method
prepareQueryOrNewObj()
ofDocumentPersister
calls theprepareQueryElement()
for each element in $newObj and the array of two ObjectId is passed togetDatabaseIdentifierValue()
that returns a new ObjectId and this is the resulting object inside the mongo db after saving:To me this makes no sense. Obviously I'm doing something wrong. What is the correct way to update a ReferenceMany property using the setNewObj in
findAndUpdate
ofQueryBuilder
? Please note that I'm not interested in alternative methods based on manipulating the php object and saving it usingpersist()
ofDocumentManager
(I know it works). In mongo shell the correct/equivalent command is: