/** * Refreshes a managed entity. * * @param array $id The identifier of the entity as an associative array from * column or field names to values. * @param object $entity The entity to refresh. */ public function refresh(array $id, $entity) { $sql = $this->_getSelectEntitiesSQL($id); $stmt = $this->_conn->executeQuery($sql, array_values($id)); $result = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); $metaColumns = array(); $newData = array(); // Refresh simple state foreach ($result as $column => $value) { $column = $this->_resultColumnNames[$column]; if (isset($this->_class->fieldNames[$column])) { $fieldName = $this->_class->fieldNames[$column]; $newValue = $this->_conn->convertToPHPValue($value, $this->_class->fieldMappings[$fieldName]['type']); $this->_class->reflFields[$fieldName]->setValue($entity, $newValue); $newData[$fieldName] = $newValue; } else { $metaColumns[$column] = $value; } } // Refresh associations foreach ($this->_class->associationMappings as $field => $assoc) { $value = $this->_class->reflFields[$field]->getValue($entity); if ($assoc['type'] & ClassMetadata::TO_ONE) { if ($value instanceof Proxy && !$value->__isInitialized__) { continue; // skip uninitialized proxies } if ($assoc['isOwningSide']) { $joinColumnValues = array(); foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { if ($metaColumns[$srcColumn] !== null) { $joinColumnValues[$targetColumn] = $metaColumns[$srcColumn]; } } if (!$joinColumnValues && $value !== null) { $this->_class->reflFields[$field]->setValue($entity, null); $newData[$field] = null; } else { if ($value !== null) { // Check identity map first, if the entity is not there, // place a proxy in there instead. $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']); if ($found = $this->_em->getUnitOfWork()->tryGetById($joinColumnValues, $targetClass->rootEntityName)) { $this->_class->reflFields[$field]->setValue($entity, $found); // Complete inverse side, if necessary. if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) { $inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']]; $targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($found, $entity); } $newData[$field] = $found; } else { // FIXME: What is happening with subClassees here? $proxy = $this->_em->getProxyFactory()->getProxy($assoc['targetEntity'], $joinColumnValues); $this->_class->reflFields[$field]->setValue($entity, $proxy); $newData[$field] = $proxy; $this->_em->getUnitOfWork()->registerManaged($proxy, $joinColumnValues, array()); } } } } else { // Inverse side of 1-1/1-x can never be lazy. //$newData[$field] = $assoc->load($entity, null, $this->_em); $newData[$field] = $this->_em->getUnitOfWork()->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity, null); } } else { if ($value instanceof PersistentCollection && $value->isInitialized()) { $value->setInitialized(false); $newData[$field] = $value; } } } $this->_em->getUnitOfWork()->setOriginalEntityData($entity, $newData); }