Fetch target entity or entities.

@param Entity $entity
@return mixed
961091
public function solveFetch(Entity &$entity)
    {
        /** @var JoinTable $joinTable */
        $joinTable = $this->relation->join;

        // Get target entity structure
        $targetStructure = EntityManager::getInstance()->getEntityStructure($joinTable->targetEntityName);

        $query = "SELECT * FROM {$targetStructure->tableName} as target, {$joinTable->name} as couple WHERE target.{$joinTable->targetColumn->entityColumn} = couple.{$joinTable->targetColumn->name} AND couple.{$joinTable->column->name} = ?;";

        $bind = array($entity->{$joinTable->column->entityColumn});
        $results = EntityManager::query($this->relation->targetEntity)->custom($query, $bind);

        return $results;
    }
794404
public function getScopedQuery($entity)
    {
        $query = $this->getRepository()->getQuery();

        foreach ($this->_scopes as $key => $value) {
            if (is_numeric($key)) {
                $key = $value;
                $value = $entity->$key;
            }

            $query->where($key, '=', $value);
        }

        return $query;
    }
1003907
public function getValidatorFor($entity)
    {
        $rules = $this->getEntityRulesFor($entity);

        return $this->validator->make($this->extract($entity), $rules->rules($entity), $rules->messages());
    }
79336
public static function update($entity)
    {
        $entityManager = self::getEntityManager();
        $entity = $entityManager->merge($entity);
        $entityManager->flush(); // transact

        return $entity;
    }
303418
public function query($entity, $query)
	{
		$retValue = array();

		if (! is_null($this->_db)){
			try{
				$mongo = $this->_db->selectCollection($entity);

				// Find by query
				$cursor = $mongo->find($query);

				if (is_null($cursor))
					$retValue = array();
				else
					$retValue = iterator_to_array($cursor);
			}
			catch(Exception $e) {
				$this->_log->addError($e->getMessage());
			}	
		}

		return $retValue;
	}
824220
private function getHeaders(Entity $entity)
    {
        yield 'ccm_date_created' => 'dateCreated';

        $attributes = $entity->getAttributes();
        foreach ($attributes as $attribute) {
            yield $attribute->getAttributeKeyHandle() => $attribute->getAttributeKeyDisplayName();
        }
    }
621790
public function & getEntityChangeSet($entity)
    {
        $oid  = spl_object_id($entity);
        $data = [];

        if (! isset($this->entityChangeSets[$oid])) {
            return $data;
        }

        return $this->entityChangeSets[$oid];
    }
528660
public function patch(Entity $entity)
    {
        if (!($entity instanceof Entity)) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Invalid argument type passed to patch. Expecting Entity got %s',
                    gettype($entity)
                )
            );
        }

        $endPointUrl = $this->getEndPointUrl(get_class($entity));

        if (method_exists($entity, 'endPointUrlArg')) {
            $endPointUrl.= '/'.$entity->endPointUrlArg();
        }

        if (method_exists($entity, 'formatForPatch')) {
            $entity = $entity->formatForPatch();
        }


        return $this->curlSend('PATCH', $entity, $endPointUrl);
    }
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();
    }
900681
protected function singleEntity($entity)
    {
        if (empty($entity)) {
            return null;
        }

        $singleEntity = $entity;

        if (is_array($entity)) {
            if (count($entity) > 1) {
                throw RuntimeException::multipleResultsFound();
            }

            $singleEntity = reset($entity);
        }

        $entityClass = $this->getEntityName();

        if (!$singleEntity instanceof $entityClass) {
            throw InvalidArgumentException::invalidEntityType(
                $this->getEntityName(),
                $singleEntity
            );
        }

        return $singleEntity;
    }
485110
public function get($entityId)
    {
        $key = $this->getEntityDescriptor()->className();
        $key .= '::'.$entityId;
        $entity = $this->getIdentityMap()->get($key, false);
        if ($entity === false) {
            $entity = $this->load($entityId);
        }
        return $entity;
    }
536796
public function getLevel($entity)
    {
        $primaryKey = $this->_getPrimaryKey();
        $id = $entity;
        if ($entity instanceof EntityInterface) {
            $id = $entity->get($primaryKey);
        }
        $config = $this->getConfig();
        $entity = $this->_table->find('all')
            ->select([$config['left'], $config['right']])
            ->where([$primaryKey => $id])
            ->first();

        if ($entity === null) {
            return false;
        }

        $query = $this->_table->find('all')->where([
            $config['left'] . ' <' => $entity[$config['left']],
            $config['right'] . ' >' => $entity[$config['right']],
        ]);

        return $this->_scope($query)->count();
    }
834038
public function find($entityId)
    {
        $this->preFind($entityId);
        $entity = $this->doFind($entityId);
        $this->postFind($entity);

        return $entity;
    }
1188245
protected function getEntityMapInstanceFor($entity)
    {
        // First, we'll try to load the entity map from cache, to
        // save from time consuming parsing of the relationships
        $entityMap = $this->getEntityMapInstanceFromCache($entity);

        if ($entityMap !== null) {
            return $entityMap;
        }

        if (class_exists($entity.'Map')) {
            $map = $entity.'Map';
            $map = new $map();

            return $map;
        }

        if ($map = $this->getMapFromNamespaces($entity)) {
            return $map;
        }

        if ($this->strictMode) {
            throw new EntityMapNotFoundException("No Map registered for $entity");
        }

        $map = $this->getNewEntityMap();

        return $map;
    }
642349
protected function getEntityTable($entity)
    {
        if (!$entity instanceof EntityInterface) {
            $entity = $this->getEntityClass($entity);
        }
        return $this->tablePrefix . $entity::getTable() . $this->tableSuffix;
    }
1229189
private function getRepository($entity)
    {
        if (!array_key_exists($entity, $this->repositories)) {
            throw new \InvalidArgumentException("'$entity' is not a valid repository. One of [".implode(', ', array_keys($this->repositories)).'] is expected.');
        }

        return $this->repositories[$entity];
    }
104292
public function getMetaData($entity)
    {
        $propertyAnnotations = [];
        $result = new \stdClass;
        $class = new \ReflectionClass($entity);
        $classAnnotations = $this->_reader->getClassAnnotations($class);
        foreach ($class->getProperties() as $key => $value) {
            $annotation = $this->_reader->getPropertyAnnotations(new \ReflectionProperty($entity, $value->name));
            if (count($annotation)) {
                $propertyAnnotations[$value->name] = array_pop($annotation);
            }
        }

        foreach ($classAnnotations as $annotation) {
            if ($annotation instanceof \MercadoPago\Annotation\RestMethod) {
                $result->methods[$annotation->method] = get_object_vars($annotation);
            }
            if ($annotation instanceof \MercadoPago\Annotation\RequestParam) {
                $result->params[] = $annotation->param;
            }
            if ($annotation instanceof \MercadoPago\Annotation\DenyDynamicAttribute) {
                $result->denyDynamicAttribute = true;
            }
        }

        foreach ($propertyAnnotations as $key => $annotation) {
            if ($annotation instanceof \MercadoPago\Annotation\Attribute) {
                $result->attributes[$key] = get_object_vars($annotation);
            }
        }

        return $result;
    }