Skip to content

Commit

Permalink
Refactoring, removing unneeded code (fixes MODM-32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Aug 7, 2010
1 parent 618896b commit 935759b
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 133 deletions.
Expand Up @@ -47,13 +47,6 @@ abstract class AbstractPersistentCollection implements Collection

protected $_mapping;

/**
* The DocumentManager that manages the persistence of the collection.
*
* @var Doctrine\ODM\MongoDB\DocumentManager
*/
protected $_dm;

/**
* Whether the collection is dirty and needs to be synchronized with the database
* when the UnitOfWork that manages its persistent state commits.
Expand All @@ -76,20 +69,17 @@ abstract class AbstractPersistentCollection implements Collection
*/
protected $_coll;

/**
* Mongo command prefix
* @var string
*/
protected $_cmd;

public function __construct(DocumentManager $dm, Collection $coll)
public function __construct(Collection $coll)
{
$this->_coll = $coll;
$this->_dm = $dm;
$this->_cmd = $dm->getConfiguration()->getMongoCmd();
}

abstract protected function _initialize();
/**
* Method for initialize the collection to be overridden in subclass.
*/
protected function _initialize()
{
}

/**
* Marks this collection as changed/dirty.
Expand Down Expand Up @@ -421,7 +411,6 @@ public function clear()
$result = $this->_coll->clear();
if ($this->_mapping->isOwningSide) {
$this->_changed();
$this->_dm->getUnitOfWork()->scheduleCollectionDeletion($this);
}
return $result;
}
Expand Down
Expand Up @@ -34,20 +34,4 @@
*/
final class PersistentEmbeddedCollection extends AbstractPersistentCollection
{
protected function _initialize()
{
}

/**
* {@inheritdoc}
*/
public function clear()
{
$this->_initialize();
$result = $this->_coll->clear();
if ($this->_mapping->isOwningSide) {
$this->_changed();
}
return $result;
}
}
Expand Up @@ -22,6 +22,7 @@
use Doctrine\Common\Collections\Collection,
Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
Doctrine\ODM\MongoDB\Proxy\Proxy,
Doctrine\ODM\MongoDB\DocumentManager,
Closure;

/**
Expand All @@ -35,8 +36,27 @@
final class PersistentReferenceCollection extends AbstractPersistentCollection
{
/**
* Initializes the collection by loading its contents from the database
* if the collection is not yet initialized.
* The DocumentManager that manages the persistence of the collection.
*
* @var Doctrine\ODM\MongoDB\DocumentManager
*/
protected $_dm;

/**
* Mongo command prefix
* @var string
*/
protected $_cmd;

public function __construct(DocumentManager $dm, Collection $coll)
{
$this->_coll = $coll;
$this->_dm = $dm;
$this->_cmd = $dm->getConfiguration()->getMongoCmd();
}

/**
* @inheritdoc
*/
protected function _initialize()
{
Expand Down Expand Up @@ -64,4 +84,18 @@ protected function _initialize()
$this->_initialized = true;
}
}

/**
* {@inheritdoc}
*/
public function clear()
{
$this->_initialize();
$result = $this->_coll->clear();
if ($this->_mapping->isOwningSide) {
$this->_changed();
$this->_dm->getUnitOfWork()->scheduleCollectionDeletion($this);
}
return $result;
}
}
35 changes: 21 additions & 14 deletions lib/Doctrine/ODM/MongoDB/Hydrator.php
Expand Up @@ -69,7 +69,7 @@ public function __construct(DocumentManager $dm)
* @param array $data The array of document data.
* @return array $values The array of hydrated values.
*/
public function hydrate($document, $data)
public function hydrate($document, &$data)
{
$metadata = $this->_dm->getClassMetadata(get_class($document));
foreach ($metadata->fieldMappings as $mapping) {
Expand All @@ -84,11 +84,11 @@ public function hydrate($document, $data)

// Hydrate embedded
if (isset($mapping['embedded'])) {
$value = $this->_hydrateEmbedded($mapping, $rawValue);
$value = $this->_hydrateEmbedded($document, $mapping, $rawValue);

// Hydrate reference
} elseif (isset($mapping['reference'])) {
$value = $this->_hydrateReference($mapping, $rawValue);
$value = $this->_hydrateReference($document, $mapping, $rawValue);

// Hydrate regular field
} else {
Expand All @@ -97,6 +97,7 @@ public function hydrate($document, $data)

// Set hydrated field value to document
if ($value !== null) {
$data[$mapping['name']] = $value;
$metadata->setFieldValue($document, $mapping['fieldName'], $value);
}
}
Expand Down Expand Up @@ -141,12 +142,14 @@ private function _executeAlsoLoadMethods($document, array $mapping, array $data)
}
}

private function _hydrateReference(array $mapping, array $reference)
private function _hydrateReference($document, array $mapping, array $reference)
{
if ($mapping['type'] === 'one' && isset($reference[$this->_cmd . 'id'])) {
return $this->_hydrateOneReference($mapping, $reference);
} elseif ($mapping['type'] === 'many' && (is_array($reference) || $reference instanceof Collection)) {
return $this->_hydrateManyReference($mapping, $reference);
$coll = $this->_hydrateManyReference($mapping, $reference);
$coll->setOwner($document, $mapping);
return $coll;
}
}

Expand All @@ -160,21 +163,24 @@ private function _hydrateOneReference(array $mapping, array $reference)

private function _hydrateManyReference(array $mapping, array $references)
{
$documents = new PersistentReferenceCollection($this->_dm, new ArrayCollection());
$documents->setInitialized(false);
$coll = new PersistentReferenceCollection($this->_dm, new ArrayCollection());
$coll->setInitialized(false);
foreach ($references as $reference) {
$document = $this->_hydrateOneReference($mapping, $reference);
$documents->add($document);
$coll->add($document);
}
return $documents;
$coll->takeSnapshot();
return $coll;
}

private function _hydrateEmbedded(array $mapping, array $embeddedDocument)
private function _hydrateEmbedded($document, array $mapping, array $embeddedDocument)
{
if ($mapping['type'] === 'one') {
return $this->_hydrateOneEmbedded($mapping, $embeddedDocument);
} elseif ($mapping['type'] === 'many') {
return $this->_hydrateManyEmbedded($mapping, $embeddedDocument);
$coll = $this->_hydrateManyEmbedded($mapping, $embeddedDocument);
$coll->setOwner($document, $mapping);
return $coll;
}
}

Expand All @@ -188,12 +194,13 @@ private function _hydrateOneEmbedded(array $mapping, array $embeddedDocument)

private function _hydrateManyEmbedded(array $mapping, array $embeddedDocuments)
{
$documents = new ArrayCollection();
$coll = new PersistentEmbeddedCollection(new ArrayCollection());
foreach ($embeddedDocuments as $embeddedDocument) {
$document = $this->_hydrateOneEmbedded($mapping, $embeddedDocument);
$documents->add($document);
$coll->add($document);
}
return new PersistentEmbeddedCollection($this->_dm, $documents);
$coll->takeSnapshot();
return $coll;
}

private function _hydrateField(array $mapping, $value)
Expand Down
7 changes: 1 addition & 6 deletions lib/Doctrine/ODM/MongoDB/MongoDBException.php
Expand Up @@ -99,9 +99,4 @@ public static function unknownDocumentNamespace($documentNamespaceAlias)
{
return new self("Unknown Document namespace alias '$documentNamespaceAlias'.");
}

public static function identifierCannotBeUpdated()
{
return new self("Document idetifier updates are not allowed");
}
}
}

0 comments on commit 935759b

Please sign in to comment.