diff --git a/src/Collection/AbstractTitleCollection.php b/src/Collection/AbstractTitleCollection.php index 33c9296..c9ba51e 100644 --- a/src/Collection/AbstractTitleCollection.php +++ b/src/Collection/AbstractTitleCollection.php @@ -148,6 +148,18 @@ public function hasCustomFields() return false; } + /** + * Determine if all items in the collection are fully hydrated + * + * @return bool + */ + public function isHydrated() + { + return $this->reduce(function ($carry, $item) { + return $carry && $item->hydrated; + }, true); + } + /** * {@inheritdoc} */ diff --git a/src/Hydrator/AbstractHydrator.php b/src/Hydrator/AbstractHydrator.php index b328697..f180f8b 100644 --- a/src/Hydrator/AbstractHydrator.php +++ b/src/Hydrator/AbstractHydrator.php @@ -73,5 +73,7 @@ public function preload(array $entryIds) /** * {@inheritdoc} */ - abstract public function hydrate(AbstractEntity $entity, AbstractProperty $property); + public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + $entity->hydrated = true; + } } diff --git a/src/Hydrator/AssetsHydrator.php b/src/Hydrator/AssetsHydrator.php index 7d35571..185d785 100644 --- a/src/Hydrator/AssetsHydrator.php +++ b/src/Hydrator/AssetsHydrator.php @@ -98,6 +98,8 @@ public function preload(array $entryIds) */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + if (isset($this->selections[$entity->getType()][$entity->getId()][$property->getId()])) { $value = $this->selections[$entity->getType()][$entity->getId()][$property->getId()]; } else { diff --git a/src/Hydrator/DateHydrator.php b/src/Hydrator/DateHydrator.php index 69864a7..ddcba93 100644 --- a/src/Hydrator/DateHydrator.php +++ b/src/Hydrator/DateHydrator.php @@ -23,6 +23,8 @@ class DateHydrator extends AbstractHydrator */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = $entity->getAttribute($property->getIdentifier()); $value = $value ? Carbon::createFromFormat('U', $value) : null; diff --git a/src/Hydrator/ExplodeHydrator.php b/src/Hydrator/ExplodeHydrator.php index 4b673fe..f723d26 100644 --- a/src/Hydrator/ExplodeHydrator.php +++ b/src/Hydrator/ExplodeHydrator.php @@ -22,6 +22,8 @@ class ExplodeHydrator extends AbstractHydrator */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = $entity->getAttribute($property->getIdentifier()); $value = $value ? explode("\n", $value) : null; diff --git a/src/Hydrator/FileHydrator.php b/src/Hydrator/FileHydrator.php index 98644f9..094aa3a 100644 --- a/src/Hydrator/FileHydrator.php +++ b/src/Hydrator/FileHydrator.php @@ -97,6 +97,8 @@ public function preload(array $entryIds) */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = $entity->getAttribute($property->getIdentifier()); $value = $value && isset($this->files[$value]) ? $this->files[$value] : null; diff --git a/src/Hydrator/GridHydrator.php b/src/Hydrator/GridHydrator.php index 2a73d28..98d8f74 100644 --- a/src/Hydrator/GridHydrator.php +++ b/src/Hydrator/GridHydrator.php @@ -123,6 +123,8 @@ public function preload(array $entryIds) */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = isset($this->sortedRows[$entity->getId()][$property->getId()]) ? $this->sortedRows[$entity->getId()][$property->getId()] : new GridRowCollection(); $entity->setAttribute($property->getName(), $value); diff --git a/src/Hydrator/MatrixHydrator.php b/src/Hydrator/MatrixHydrator.php index 9e28bc1..d39b4c5 100644 --- a/src/Hydrator/MatrixHydrator.php +++ b/src/Hydrator/MatrixHydrator.php @@ -115,6 +115,8 @@ public function preload(array $entryIds) */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = isset($this->sortedRows[$entity->getId()][$property->getId()]) ? $this->sortedRows[$entity->getId()][$property->getId()] : new MatrixRowCollection(); $entity->setAttribute($property->getName(), $value); diff --git a/src/Hydrator/ParentsHydrator.php b/src/Hydrator/ParentsHydrator.php index cbc5fad..f529142 100644 --- a/src/Hydrator/ParentsHydrator.php +++ b/src/Hydrator/ParentsHydrator.php @@ -61,6 +61,8 @@ public function __construct(ConnectionInterface $db, EntryCollection $collection */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $entries = isset($this->entries[$entity->getId()]) ? $this->entries[$entity->getId()] : array(); $value = $this->relationshipCollection->createChildCollection($entries); diff --git a/src/Hydrator/PipeHydrator.php b/src/Hydrator/PipeHydrator.php index 42c752c..eb21a13 100644 --- a/src/Hydrator/PipeHydrator.php +++ b/src/Hydrator/PipeHydrator.php @@ -22,6 +22,8 @@ class PipeHydrator extends AbstractHydrator */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = $entity->getAttribute($property->getIdentifier()); $value = $value ? explode("|", $value) : null; diff --git a/src/Hydrator/PlayaHydrator.php b/src/Hydrator/PlayaHydrator.php index 082735c..4b5026d 100644 --- a/src/Hydrator/PlayaHydrator.php +++ b/src/Hydrator/PlayaHydrator.php @@ -78,6 +78,8 @@ public function __construct(ConnectionInterface $db, EntryCollection $collection */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $entries = isset($this->entries[$entity->getType()][$entity->getId()][$property->getId()]) ? $this->entries[$entity->getType()][$entity->getId()][$property->getId()] : array(); diff --git a/src/Hydrator/RelationshipHydrator.php b/src/Hydrator/RelationshipHydrator.php index 9de866d..08def79 100644 --- a/src/Hydrator/RelationshipHydrator.php +++ b/src/Hydrator/RelationshipHydrator.php @@ -65,6 +65,8 @@ public function __construct(ConnectionInterface $db, EntryCollection $collection */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $entries = isset($this->entries[$entity->getType()][$entity->getId()][$property->getId()]) ? $this->entries[$entity->getType()][$entity->getId()][$property->getId()] : array(); diff --git a/src/Hydrator/SiblingsHydrator.php b/src/Hydrator/SiblingsHydrator.php index a6142f4..6639737 100644 --- a/src/Hydrator/SiblingsHydrator.php +++ b/src/Hydrator/SiblingsHydrator.php @@ -61,6 +61,8 @@ public function __construct(ConnectionInterface $db, EntryCollection $collection */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = isset($this->entries[$entity->getId()]) ? $this->entries[$entity->getId()] : array(); $value = $this->relationshipCollection->createChildCollection($entries); diff --git a/src/Hydrator/WysiwygHydrator.php b/src/Hydrator/WysiwygHydrator.php index febe6cc..355082f 100644 --- a/src/Hydrator/WysiwygHydrator.php +++ b/src/Hydrator/WysiwygHydrator.php @@ -59,6 +59,8 @@ public function __construct(ConnectionInterface $db, EntryCollection $collection */ public function hydrate(AbstractEntity $entity, AbstractProperty $property) { + parent::hydrate($entity, $property); + $value = $this->parse($entity->getAttribute($property->getIdentifier())); $entity->setAttribute($property->getName(), $value); diff --git a/src/Model/AbstractEntity.php b/src/Model/AbstractEntity.php index c3a9150..15cd403 100644 --- a/src/Model/AbstractEntity.php +++ b/src/Model/AbstractEntity.php @@ -14,6 +14,13 @@ */ abstract class AbstractEntity extends Model { + + /** + * Entity hydration state + * @var bool + */ + protected $hydrated = false; + /** * Get the entity ID (eg. entry_id or row_id) * @return string|int diff --git a/src/Model/Entry.php b/src/Model/Entry.php index be04db0..e68c55e 100644 --- a/src/Model/Entry.php +++ b/src/Model/Entry.php @@ -67,7 +67,7 @@ public function newCollection(array $models = array()) $collection = call_user_func($method, $models, self::$channelRepository, self::$fieldRepository); - if ($models) { + if ($models && !$collection->isHydrated()) { $this->hydrateCollection($collection); } diff --git a/src/Model/Title.php b/src/Model/Title.php index 146f765..bcb1a99 100644 --- a/src/Model/Title.php +++ b/src/Model/Title.php @@ -267,7 +267,7 @@ public function newCollection(array $models = array()) /** * Loop through all the hydrators to set Entry custom field attributes - * @param \rsanchez\Deep\Collection\TitleCollection $collection + * @param \rsanchez\Deep\Collection\AbstractTitleCollection $collection * @return void */ public function hydrateCollection(AbstractTitleCollection $collection)