/** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { $parentTable = $this->parentMap->getTable(); $this->setJoin(); if (static::$constraints) { $farParentKeyName = $this->farParentMap->getKeyName(); $this->query->where($parentTable . '.' . $this->firstKey, '=', $this->farParent->getEntityAttribute($farParentKeyName)); } }
/** * Add the Entity primary key if not in requested columns * * @param array $columns * @return array */ protected function enforceIdColumn($columns) { if (!in_array($this->entityMap->getKeyName(), $columns)) { $columns[] = $this->entityMap->getKeyName(); } return $columns; }
/** * Check for entity existence in the EntityCache * * @return boolean */ public function exists() { if (!$this->hasPrimaryKeyDefined()) { $this->exists = false; } else { $cache = $this->mapper->getEntityCache(); $key = $this->entityMap->getKeyName(); // Check if we have a cache record for the entity // which have to be the case. if ($cache->has($this->entity->getEntityAttribute($key))) { $this->exists = true; } else { $this->exists = false; } } return $this->exists; }
/** * Get all of the primary keys for an array of entities. * * @param array $entities * @param string $key * @return array */ protected function getKeys(array $entities, $key = null) { if (is_null($key)) { $key = $this->relatedMap->getKeyName(); } return array_unique(array_values(array_map(function ($value) use($key) { return $value->getEntityAttribute($key); }, $entities))); }
/** * Build the Entity(ies) * * @param array $results * @return Mappable|EntityCollection */ public function build(array $results) { $entities = array(); $prototype = $this->mapper->newInstance(); $keyName = $this->entityMap->getKeyName(); $tmpCache = []; foreach ($results as $result) { $instance = clone $prototype; $resultArray = (array) $result; $tmpCache[$resultArray[$keyName]] = $resultArray; // Hydrate any embedded Value Object $this->hydrateValueObjects($resultArray); $instance->setEntityAttributes($resultArray); // Hydrate relation attributes with lazyloading proxies if (count($this->lazyLoads) > 0) { $proxies = $this->getLazyLoadingProxies($instance); $instance->setEntityAttributes($resultArray + $proxies); } $entities[] = $instance; } $this->mapper->getEntityCache()->add($tmpCache); return $entities; }