Overview

Namespaces

  • rsanchez
    • Deep
      • App
        • EE
        • Laravel
          • Facade
      • Collection
      • Hydrator
      • Model
      • Plugin
      • Relations
      • Repository

Classes

  • AbstractHydrator
  • AssetsHydrator
  • DateHydrator
  • ExplodeHydrator
  • FileHydrator
  • GridHydrator
  • HydratorFactory
  • MatrixHydrator
  • ParentsHydrator
  • PipeHydrator
  • PlayaHydrator
  • RelationshipHydrator
  • SiblingsHydrator
  • WysiwygHydrator

Interfaces

  • HydratorInterface
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: 
 3: /**
 4:  * Deep
 5:  *
 6:  * @package      rsanchez\Deep
 7:  * @author       Rob Sanchez <info@robsanchez.com>
 8:  */
 9: 
10: namespace rsanchez\Deep\Hydrator;
11: 
12: use Illuminate\Database\Eloquent\Model;
13: use rsanchez\Deep\Collection\EntryCollection;
14: use rsanchez\Deep\Collection\RelationshipCollection;
15: use rsanchez\Deep\Model\AbstractProperty;
16: use rsanchez\Deep\Model\AbstractEntity;
17: use rsanchez\Deep\Hydrator\AbstractHydrator;
18: use rsanchez\Deep\Model\RelationshipEntry;
19: 
20: /**
21:  * Hydrator for the Relationship fieldtype
22:  */
23: class RelationshipHydrator extends AbstractHydrator
24: {
25:     /**
26:      * @var \rsanchez\Deep\Model\RelationshipEntry
27:      */
28:     protected $model;
29: 
30:     /**
31:      * {@inheritdoc}
32:      */
33:     public function __construct(EntryCollection $collection, $fieldtype, RelationshipEntry $model)
34:     {
35:         parent::__construct($collection, $fieldtype);
36: 
37:         $this->model = $model;
38: 
39:         $this->relationshipCollection = $this->model->parentEntryId($collection->modelKeys())->get();
40: 
41:         foreach ($this->relationshipCollection as $entry) {
42:             $type = $entry->grid_field_id ? 'grid' : 'entry';
43:             $entityId = $entry->grid_field_id ? $entry->grid_row_id : $entry->parent_id;
44:             $propertyId = $entry->grid_field_id ? $entry->grid_col_id : $entry->field_id;
45: 
46:             if (! isset($this->entries[$type][$entityId][$propertyId])) {
47:                 $this->entries[$type][$entityId][$propertyId] = array();
48:             }
49: 
50:             $this->entries[$type][$entityId][$propertyId][] = $entry;
51:         }
52: 
53:         // add these entry IDs to the main collection
54:         $collection->addEntryIds($this->relationshipCollection->modelKeys());
55:     }
56: 
57:     /**
58:      * {@inheritdoc}
59:      */
60:     public function hydrate(AbstractEntity $entity, AbstractProperty $property)
61:     {
62:         $entries = isset($this->entries[$entity->getType()][$entity->getId()][$property->getId()])
63:             ? $this->entries[$entity->getType()][$entity->getId()][$property->getId()] : array();
64: 
65:         $value = $this->relationshipCollection->createChildCollection($entries);
66: 
67:         $entity->setAttribute($property->getName(), $value);
68: 
69:         return $value;
70:     }
71: }
72: 
API documentation generated by ApiGen 2.8.0