Lazy loading of relations callback method

@param Entity $entity
@return Entity|RecordList
546477
public function load(Entity $entity)
    {
        /** @var \Slick\Orm\Sql\Select $sql */
        $sql = call_user_func_array(
            array($this->getRelatedEntity(), 'find'),
            []
        );
        $pmk = $this->getEntity()->getPrimaryKey();
        $table = Entity\Manager::getInstance()->get($this->getRelatedEntity())
            ->getEntity()->getTableName();
        $sql->where(
            [
                "{$table}.{$this->getForeignKey()} = :id" => [
                    ':id' => $this->getEntity()->$pmk
                ]
            ]
        );
        return $sql->first();
    }
1136374
protected function eagerLoadRelations(array $entities)
    {
        if (empty($entities)) {
            return;
        }

        if ($this->class === null) {
            throw new LogicException('A class must be specified for Eager Loading using the `asClass` method.');
        }

        foreach ($this->with as $method) {
            $relation = $this->getRelation($entities[0], $method);
            $relation->addEagerConstraints($entities);
            $relationEntities = $relation->getEager();
            /** @var Model $entity */
            foreach ($entities as $entity) {
                $id = $entity->getId();
                $entity->setRelationEntities($method, isset($relationEntities[$id]) ? $relationEntities[$id] : null);
            }
        }
    }
923167
protected function generateExtensionMethods(Entity $entity)
    {
        $methods    = [];
        $descriptor = $entity->getDescriptor();
        $extensions = $descriptor->getExtensionList() ?: [];

        foreach ($extensions as $field) {
            $methods[] = $this->generateExtensionMethod($entity, $field);
        }

        return $methods;
    }
300300
private function getLoader($entity)
    {
        $entityClass = get_class($entity);

        if ($this->unsupportedClasses->containsKey($entityClass)) {
            return;
        }
        if ($this->loaders->containsKey($entityClass)) {
            return $this->loaders->get($entityClass);
        }
        foreach ($this->loaders as $handledClass => $loader) {
            if (is_a($entity, $handledClass)) {
                $this->loaders->set($entityClass, $loader);

                return $loader;
            }
        }

        $this->unsupportedClasses->set($entityClass, false);
    }
361923
public function incoming(Entity\Content $entity)
    {
        return $this->filter(function ($el) use ($entity) {
            /** @var Entity\Relations $el */
            return $el->getToContenttype() == (string) $entity->getContenttype() && $el->getToId() === $entity->getId();
        });
    }
232292
public function delete(Entity $entity)
    {
        $this->delete = new Delete('history');
        $this->delete->where(array(
             'history.history_id = ?' => $entity->getHistoryId()
        ));
                 
        return $this->deleteRow();
    }
568720
public function setEntity(Entity $entity)
    {
        /** @var SharedEventManager $events */
        $events = $this->getContainer()->get('sharedEventManager');
        $events->attach(
            get_class($entity),
            Save::BEFORE_SAVE,
            [$this, 'beforeSave']
        );
        $events->attach(
            get_class($entity),
            Select::BEFORE_SELECT,
            [$this, 'beforeSelect']
        );
        $events->attach(
            get_class($entity),
            Select::AFTER_SELECT,
            [$this, 'afterSelect']
        );
        $this->getContainer()->set('sharedEventManager', $events);
        return parent::setEntity($entity);
    }
1018276
protected function assignRelationships($entity, $attributes)
    {
        $modelAttributes = $this->model->getAttributes($entity);

        // We'll filter through all of the columns, and check
        // to see if there are any defined relationships. If there
        // are, then we'll need to create those records as well.

        foreach ($modelAttributes as $columnName => $value) {
            if ($relationship = $this->findRelation($value)) {
                $entity[$columnName] = $this->fetchRelationId($relationship, $columnName, $attributes);
            }
        }

        return $entity;
    }
386343
public function reload($entity)
    {
        //Get meta and hash for entity
        $hash = $this->getHash($entity);
        $meta = $this->getMeta($entity);

        //Get the primary key
        $id = $meta->getPrimaryKey()->getValue($entity);

        if($meta instanceof NodeMeta)
        {
            //Everyman Node is cached, just load it
            if(isset($this->everymanNodeCache[$hash]))
            {
                return $this->loadNode($this->everymanNodeCache[$hash]);
            }

            //Not in the cache, but has an id, get the node and load it.
            elseif($id)
            {
                $node = $this->client->getNode($id);
                return $this->loadNode($node);
            }

            else
            {
                throw new Exception('Cannot reload an unsaved node.');
            }
        }

        else
        {
            if(isset($this->everymanRelationCache[$hash]))
            {
                return $this->loadRelation($this->everymanRelationCache[$hash]);
            }

            //Not in the cache, but has an id, get the node and load it.
            elseif($id)
            {
                $rel = $this->client->getRelationship($id);
                return $this->loadRelation($rel);
            }

            else
            {
                throw new Exception('Cannot reload an unsaved relation.');
            }
        }
    }
934906
public function formList($entity)
    {
        if ($entity = Entity::fromYamlFile($entity)) {
            $list = new FormList($entity);

            return $list->view();
        } else {
            abort(404);
        }
    }
535800
public function call_previous(\Asgard\Entity\Entity $entity) {
		$orm = $entity::where(['position < ?' => $entity->position])->orderBy('position DESC');
		if($this->category)
			$orm->where($this->category, $entity->get($this->category));

		return $orm->first();
	}
669512
public function refresh($entity)
    {
        $query = $this->db->table($entity->definition()->getName());
        $primary = $entity->id();
        foreach ($primary as $k => $v) {
            $query->filter($k, $v);
        }
        $data = $query[0] ?? [];
        $entity->fromArray($data);
        return $this->lazy($entity, $data);
    }
27136
protected function cacheForgetByKeys($entity = null)
    {
        $keys = $this->keysToForgetCache;

        if (is_array($keys) and array_diff_key($keys, array_keys(array_keys($keys)))) {
            foreach ($keys as $key => $method) {
                cache()->forget($key);
                if (is_subclass_of($entity, ParentModel::class)) {
                    if (method_exists($model = $entity->getModel(), $method)) {
                        $model->$method();
                    }
                }
            }
        }
    }
1140665
protected function buildTable(Entity $entity)
    {
        // Prepare pivot tables and one-to-* columns
        $attributes = $this->prepareRelationships($entity) + $entity->getAttributeConfig();

        // Allow other services to append schema
        foreach(event(new SchemaPrepare($entity)) as $attrs){

            foreach((array)$attrs as $key => $value){

                $attributes[$key] = $this->applyAttributeDefaults($value);

            }

        }

        // Append attribute columns
        return $this->addAttributes($entity->getTable(), $attributes);
    }
1112607
protected function getRelationClosure(Entity $entity, Attribute $attribute)
    {
        $method = $attribute->is_collection ? 'hasMany' : 'hasOne';

        // This will return a closure fully binded to the current entity instance,
        // which will help us to simulate any relation as if it was made in the
        // original entity class definition using a function statement.
        return Closure::bind(function () use ($entity, $attribute, $method) {
            $relation = $entity->$method($attribute->getAttribute('type'), 'entity_id', 'id');

            // Since an attribute could be attached to multiple entities, then values could have
            // same entity ID, but for different entity types, so we need to add type where
            // clause to fetch only values related to the given entity ID + entity type.
            $relation->where('entity_type', get_class($entity));

            // We add a where clause in order to fetch only the elements that are
            // related to the given attribute. If no condition is set, it will
            // fetch all the value rows related to the current entity.
            return $relation->where($attribute->getForeignKey(), $attribute->getKey());
        }, $entity, get_class($entity));
    }
410084
public function init(Entity $entity) {
    // check entity
    if ($entity->getEntityName() !== ($class = $this->entityMeta->getClass())) {
      throw new \InvalidArgumentException('Das übergebene Entity für diesen Synchronizer kann nur vom Typ: '.$class.' sein. Ein Entity des Typs '.$entity->getEntityName().' wurde übergeben');
    }
    
    // check relation
    $collectionMeta = $this->getCollectionPropertyMeta();
    if (!$collectionMeta->isRelation()) {
      throw new \InvalidArgumentException(
        sprintf('Das Property %s von %s ist keine Collection. Das Property muss ManyToMany oder OneToMany sein',
                $this->collectionName, $this->entityMeta->getClass())
      );
    }
  }
1172604
public function getLazy($entity, $name)
    {
        // Verify if virtual property exists
        if (! in_array($name, $this->getEntityStructure($entity)->relationProperties)) {
            throw new RelationException("Property '".$name."' is not a valid and declared property, or relation property!");
        }

        return RelationManager::with($entity)->fetch($name);
    }