Initialize primary keys and entities

@param string $constraints
@param string $joins
@param string $sorting
353057
protected function initEntities($constraints = '', $joins = '', $sorting = '')
    {
        $this->items = [];

        $query = implode(' ', [
            'SELECT t0.* FROM', '`' . $this->table . '` AS t0'
        ]);

        $statement = $this->adapter->getDriver()->query($query . $joins . $constraints . $sorting);
        $class = $this->adapter->getSchema($this->table, 'class');

        do {
            $row = $statement->fetchAssoc();

            if (is_array($row)) {
                /** @var \Rio\AbstractEntity $entity */
                $entity = new $class($this->adapter);
                $entity->fromArray($row);

                $this->items[$row[$this->adapter->getSchema($this->table, 'primary')]] = $entity;
            }
        } while (false !== $row);
    }
1101793
protected function loadHasOneOrMany(
        Collection $relations,
        HasOneOrMany $relationClass,
        Model $relationModel,
        Model $model,
        Builder $query,
        $direction
    ) {
        $ownerTable = $model->getTable();
        $foreignTable = $relationModel->getTable();

        $ownerColumn = $relationClass->getQualifiedForeignKeyName();
        $foreignColumn = $relationClass->getQualifiedParentKeyName();
        $sortedColumnRaw = '`'.$foreignTable.'`.`'.$relations->last().'`';
        $sortedColumnAlias = implode('__', [$foreignTable, $relations->last()]);

        $this->sortedColumnAlias = $sortedColumnAlias;

        $query
            ->addSelect([DB::raw($sortedColumnRaw.' AS '.$sortedColumnAlias)])
            ->join($foreignTable, $foreignColumn, '=', $ownerColumn, 'left');
    }
1023997
private function applyJoinsAndFilters(ClassMetadata $metadata, string $alias, InputObjectType $typeFields, array $joins, array $conditions): void
    {
        $this->applyJoins($metadata, $joins, $alias);
        $this->collectDqlConditions($metadata, $conditions, $typeFields, $alias);
    }
959852
@NonNull
    @Override
    public Joins join(@NonNull Join... joins) {
        if (joins == null) {
            throw new IllegalArgumentException("joins cannot be null.");
        }
        return new Joins(this, Arrays.asList(joins));
    }
884330
protected function cacheResults(array $results)
    {
        $mapper = $this->mapper;

        // When hydrating EmbeddedValue object, they'll likely won't
        // have a primary key set.
        if ($mapper->getEntityMap()->getKeyName() !== null) {
            $mapper->getEntityCache()->add($results);
        }
    }
247264
public function searchBy(
        $conditions = [],
        $sorts = [],
        $joins = [],
        $pager = null,
        $mode = 'and',
        $expression = null
    ) {
        if (!$expression) {
            return $this->search($conditions, $sorts, $joins, $pager, $mode);
        }

        $queryBuilder = $this->createQueryBuilder('o')->select('o');

        $this->queryBuilderExpressionAndConditions($queryBuilder, $conditions, $expression, $mode);
        $this->queryBuilderSorts($queryBuilder, $sorts);
        $this->queryBuilderJoins($queryBuilder, $joins);
        $this->queryBuilderPager($queryBuilder, $pager);

        return $queryBuilder->getQuery()->getResult();
    }
1070798
public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false)
    {
        $field_id = $this->get('id');

        if (self::isFilterRegex($data[0])) {
            $this->buildRegexSQL($data[0], array('value'), $joins, $where);
        } elseif (self::isFilterSQL($data[0])) {
            $this->buildFilterSQL($data[0], array('value'), $joins, $where);
        } else {
            if (is_array($data)) {
                $data = $data[0];
            }

            $this->_key++;
            $data = $this->cleanValue($data);
            $joins .= "
                LEFT JOIN
                    `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
                    ON (e.id = t{$field_id}_{$this->_key}.entry_id)
            ";
            $where .= "
                AND MATCH (t{$field_id}_{$this->_key}.value) AGAINST ('{$data}' IN BOOLEAN MODE)
            ";
        }

        return true;
    }
946566
public function addConstraints()
	{
		$parentTable = $this->parent->getTable();

		$localValue = $this->farParent[$this->localKey];

		$this->setJoin();

		if (static::$constraints) {
			$this->query->where($parentTable.'.'.$this->firstKey, '=', $localValue);
		}
	}
973213
public function orderByRaw(string $sql, array $bindings = []): BaseQueryRelateContract
    {
        $this->query->orderByRaw($sql, $bindings);

        return $this;
    }
160383
public function addEagerConstraints(array $results)
    {
        $this->query->whereIn($this->getForeignKey(), $this->getKeysFromResults($results));
    }
428990
public function addConstraints()
    {
        $this->performJoins();

        if (static::$constraints) {
            $localValue = $this->parent[$this->getFirstForeignKeyName()];

            $this->query->where($this->getQualifiedFirstLocalKeyName(), '=', $localValue);
        }
    }
292046
public function constrainQuery(EloquentBuilder &$query, $relatedModel, $constraint)
	{
		//if the column hasn't been joined yet, join it
		if (!$this->validator->isJoined($query, $this->getOption('table')))
		{
			$query->join($this->getOption('table'), $relatedModel->getTable().'.'.$relatedModel->getKeyName(), '=', $this->getOption('column2'));
		}

		$query->where($this->getOption('table').'.'.$this->getOption('column'), '=', $constraint);
	}
1066740
protected function handleJoins(array $joins)
    {
        if (empty($joins)) {
            return '';
        }
        $sql = [];
        foreach ($joins as $join) {
            /** @var Join $joinObject */
            $joinObject = $join['join'];

            $on = '';
            if ($joinObject) {
                $on = $this->handleJoinConditions($joinObject->getJoinConditions());
            }
            if ($on !== '') {
                $on = ' ON ' . $on;
            }

            $sql[] = $join['type'] . ' JOIN ' . $this->handleTables($join['table']) . $on;
        }
        return ' ' . implode(' ', $sql);
    }
1183423
protected function assignJoins(QueryBuilder $queryBuilder, array $joins = null)
    {
        if (null === $joins) {
            $joins = $this->getJoins();
        }

        foreach ($joins as $entity => $join) {
            switch ($join->getJoinMethod()) {
                case DoctrineJoinDescriptor::JOIN_METHOD_LEFT:
                    $queryBuilder->leftJoin(
                        $join->getJoin() ?: $entity,
                        $this->encodeAlias($entity),
                        $join->getJoinConditionMethod(),
                        $join->getJoinCondition()
                    );
                    break;
                case DoctrineJoinDescriptor::JOIN_METHOD_INNER:
                    $queryBuilder->innerJoin(
                        $join->getJoin() ?: $entity,
                        $this->encodeAlias($entity),
                        $join->getJoinConditionMethod(),
                        $join->getJoinCondition()
                    );
                    break;
            }
        }
    }
160794
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->getDefaultFor($model));
        }

        return $models;
    }
602632
private function loadRelations(
        LoadingContext $context,
        array $rows,
        array $objects,
        array &$objectProperties
    ) {
        $isLazyLoadingEnabled = $this->definition->isLazyLoadingEnabled();

        foreach ($this->definition->getToOneRelationMappings() as $relationMapping) {
            $relation = $relationMapping->getRelation();
            $accessor = $relationMapping->getAccessor();

            $map       = new ParentChildMap($this->primaryKeyColumnName);
            $rowKeyMap = new \SplObjectStorage();

            foreach ($rows as $key => $row) {
                $map->add($row, null);
                $rowKeyMap[$row] = $key;
            }

            $relation->load($context, $map);

            foreach ($map->getItems() as $item) {
                $objectKey = $rowKeyMap[$item->getParent()];
                $accessor->set($objects[$objectKey], $objectProperties[$objectKey], $item->getChild());
            }
        }

        foreach ($this->definition->getToManyRelationMappings() as $relationMapping) {

            $relation = $relationMapping->getRelation();
            $accessor = $relationMapping->getAccessor();

            $map       = new ParentChildrenMap($this->primaryKeyColumnName);
            $rowKeyMap = new \SplObjectStorage();

            foreach ($rows as $key => $row) {
                $map->add($row, []);
                $rowKeyMap[$row] = $key;
            }

            $hasLoaded = false;

            if (!$isLazyLoadingEnabled) {
                $relation->load($context, $map);
                $hasLoaded = true;
            }

            foreach ($map->getItems() as $item) {
                $objectKey = $rowKeyMap[$item->getParent()];
                $object    = $objects[$objectKey];

                if ($hasLoaded) {
                    $collection = $relation->buildCollection($item->getChildren());
                } else {
                    $collection = LazyCollectionFactory::from(
                        $relation->buildCollection([]),
                        function () use (&$hasLoaded, $relation, $context, $map, $item) {
                            if (!$hasLoaded) {
                                $relation->load($context, $map);
                                $hasLoaded = true;
                            }

                            return $item->getChildren();
                        }
                    );

                    $collection->appendLazyMetadata([
                        'parent'   => $object,
                        'relation' => $relation,
                    ]);
                }

                $accessor->set($object, $objectProperties[$objectKey], $collection);
            }

            unset($hasLoaded);
        }
    }
100828
public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false)
    {
        if (self::isFilterRegex($data[0])) {
            $this->buildRegexSQL($data[0], array('value'), $joins, $where);
        } elseif (self::isFilterSQL($data[0])) {
            $this->buildFilterSQL($data[0], array('value'), $joins, $where);
        } else {
            $parsed = array();

            // For the filter provided, loop over each piece
            foreach ($data as $string) {
                $type = self::parseFilter($string);

                if ($type == self::ERROR) {
                    return false;
                }

                if (!is_array($parsed[$type])) {
                    $parsed[$type] = array();
                }

                $parsed[$type][] = $string;
            }

            foreach ($parsed as $value) {
                $this->buildRangeFilterSQL($value, $joins, $where, $andOperation);
            }
        }

        return true;
    }