/** * {@inheritdoc} * * @param object $sourceEntity the entity source of this association * @param object $targetEntity the entity to load data in * @param EntityManager $em * @param array $joinColumnValues Values of the join columns of $sourceEntity. */ public function load($sourceEntity, $targetEntity, $em, array $joinColumnValues = array()) { $targetClass = $em->getClassMetadata($this->targetEntityName); if ($this->isOwningSide) { $inverseField = isset($targetClass->inverseMappings[$this->sourceEntityName][$this->sourceFieldName]) ? $targetClass->inverseMappings[$this->sourceEntityName][$this->sourceFieldName]->sourceFieldName : false; // Mark inverse side as fetched in the hints, otherwise the UoW would // try to load it in a separate query (remember: to-one inverse sides can not be lazy). $hints = array(); if ($inverseField) { $hints['fetched'][$targetClass->name][$inverseField] = true; if ($targetClass->subClasses) { foreach ($targetClass->subClasses as $targetSubclassName) { $hints['fetched'][$targetSubclassName][$inverseField] = true; } } } /* cascade read-only status if ($em->getUnitOfWork()->isReadOnly($sourceEntity)) { $hints[Query::HINT_READ_ONLY] = true; } */ $targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($joinColumnValues, $targetEntity, $this, $hints); if ($targetEntity !== null && $inverseField && !$targetClass->isCollectionValuedAssociation($inverseField)) { $targetClass->reflFields[$inverseField]->setValue($targetEntity, $sourceEntity); } } else { $conditions = array(); $sourceClass = $em->getClassMetadata($this->sourceEntityName); $owningAssoc = $targetClass->getAssociationMapping($this->mappedBy); // TRICKY: since the association is specular source and target are flipped foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) { if (isset($sourceClass->fieldNames[$sourceKeyColumn])) { $conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); } else { throw MappingException::joinColumnMustPointToMappedField($sourceClass->name, $sourceKeyColumn); } } $targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($conditions, $targetEntity, $this); if ($targetEntity !== null) { $targetClass->setFieldValue($targetEntity, $this->mappedBy, $sourceEntity); } } return $targetEntity; }
/** * Loads entities in $targetCollection using $em. * The data of $sourceEntity are used to restrict the collection * via the join table. * * @param object The owner of the collection. * @param object The collection to populate. * @param array */ public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array()) { $sourceClass = $em->getClassMetadata($this->sourceEntityName); $joinTableConditions = array(); if ($this->isOwningSide) { foreach ($this->relationToSourceKeyColumns as $relationKeyColumn => $sourceKeyColumn) { // getting id if (isset($sourceClass->fieldNames[$sourceKeyColumn])) { $joinTableConditions[$relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); } else { throw MappingException::joinColumnMustPointToMappedField($sourceClass->name, $sourceKeyColumn); } } } else { $owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedBy]; // TRICKY: since the association is inverted source and target are flipped foreach ($owningAssoc->relationToTargetKeyColumns as $relationKeyColumn => $sourceKeyColumn) { // getting id if (isset($sourceClass->fieldNames[$sourceKeyColumn])) { $joinTableConditions[$relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity); } else { throw MappingException::joinColumnMustPointToMappedField($sourceClass->name, $sourceKeyColumn); } } } $persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName); $persister->loadManyToManyCollection($this, $joinTableConditions, $targetCollection); }