Create Query Builder

@param  $model
@return bool|QueryBuilder
493642
public static function find($model)
    {
        if (property_exists(self::className(), 'instance')) {
            if (static::$instance instanceof static) {
                /**
                 * @var QueryBuilder $instance
                 */
                $instance = static::$instance;
                $instance->setDao(Dao::component())
                    ->setConditions()
                    ->setGroupFields()
                    ->setOrders()
                    ->setLimit()
                    ->setSelect()
                    ->setAsArray();

                return $instance;
            } else {
                $instance = new static($model);
                $instance->is_single = true;
                return (static::$instance = $instance);
            }
        }
        return null;
    }
1026016
protected function prepareQuery($model)
    {
        // Set the relationships that should be eager loaded
        if (! empty($this->relations)) {
            $model = $model->with($this->relations);
        }

        // Add a basic where clause to the query
        foreach ($this->where as $where) {
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);

            $model = $model->where($attribute, $operator, $value, $boolean);
        }

        // Add a "where in" clause to the query
        foreach ($this->whereIn as $whereIn) {
            list($attribute, $values, $boolean, $not) = array_pad($whereIn, 4, null);

            $model = $model->whereIn($attribute, $values, $boolean, $not);
        }

        // Add a "where not in" clause to the query
        foreach ($this->whereNotIn as $whereNotIn) {
            list($attribute, $values, $boolean) = array_pad($whereNotIn, 3, null);

            $model = $model->whereNotIn($attribute, $values, $boolean);
        }

        // Set the "offset" value of the query
        if ($this->offset > 0) {
            $model = $model->offset($this->offset);
        }

        // Set the "limit" value of the query
        if ($this->limit > 0) {
            $model = $model->limit($this->limit);
        }

        // Add an "order by" clause to the query.
        if (! empty($this->orderBy)) {
            list($attribute, $direction) = $this->orderBy;

            $model = $model->orderBy($attribute, $direction);
        }

        return $model;
    }
1171079
public function check($model)
    {
        if ($this->isSubQuery()) {
            return Builder::make($model, $this->property)->check();
        }

        switch ($this->operator) {
            case '=':
                return $this->attribute($model) === $this->value();
            break;
            case '>=':
                return $this->attribute($model) >= $this->value();
            break;
            case '<=':
                return $this->attribute($model) <= $this->value();
            break;
            case '<>':
                return $this->attribute($model) !== $this->value();
            break;
            case '>':
                return $this->attribute($model) > $this->value();
            break;
            case '<':
                return $this->attribute($model) < $this->value();
            break;
            case 'like':
                $pattern = preg_quote($this->value());
                $pattern = str_replace('%', '(.*?)', $pattern);
                $pattern = str_replace('_', '(.)', $pattern);
                $pattern = '/^'.$pattern.'$/s';

                return preg_match($pattern, $this->attribute($model)) != false;
            break;
        }
        throw new BadMethodCallException('Operator not supported: '.$this->operator);
    }
850787
public function apply(Builder $query, Model $model)
    {
        Models::scope()->applyToModelQuery($query, $model->getTable());
    }
874140
public function getQueryBuilder($entity): QueryBuilderInterface
    {
        $metaInformation = $this->metaInformationFactory->loadInformation($entity);

        return new QueryBuilder($this, $metaInformation);
    }
1123434
public function search($model)
    {
        $method = 'search';

        $condition = $this->condition ?: [[]];

        $params = $this->buildParams('limit', 'offset');

        $result = $this->call($model, $method, $condition, $params);

        //Reset params for future queries.
        $this->resetParams('limit', 'offset', 'condition');

        return $this->makeResponse($result);
    }
481026
public function getQueryBuilder()
    {
        if (!isset($this->queryBuilder)) {
            $this->queryBuilder = $this->getGeneratorConfig()->getConfiguredBuilder($this->getTable(), 'query');
        }

        return $this->queryBuilder;
    }
786535
private function getBuilder($query)
    {
        $queryType = $query->getType();

        if (!isset($this->builders[$queryType])) {
            throw new \InvalidArgumentException("Unknow query builder for {$queryType}.");
        }

        return $this->builders[$queryType];
    }
19457
public function insertFrom($model)
	{
		$db = $this->getDb();
		$myCols = array_keys( $this->schemaColumnsGet() );
		$theirCols = $model->getColumnAliases();
		$columns = '('.implode( ',', array_map_val( array_intersect( $theirCols, $myCols ), $this->quoteColumnDg() ) ).')';
		$table = $this->quoteTable();
		$db->exec('INSERT INTO '.$table.' '.$columns.' '.$model->getSQL());
		$this->id = $this->getLastInsertId();
		if( $this->recordExists() )
		{
			return $this->filterById($this->id)->loadOne();
		}
		else
		{
			return $this;
		}
	}
311761
public function newQuery()
    {
        $builder = $this->newElicitBuilder(
            $this->newBaseQueryBuilder()
        );

        // Once we have the query builders, we will set the model instances so the
        // builder can easily access any information it may need from the model
        // while it is constructing and executing various queries against it.
        $builder->setModel($this);

        return $builder;
    }
1353165
public function create($model, $data)
	{
		$params = $this->buildParams(array(
			$model,
			'create',
			$data
		));

		$response = $this->getClient('object')->call('execute', $params);

		return $response;
	}
1201351
public function createModel($model)
    {
        $schema = new Schema();

        $tableDef = $schema->createTable($model->getMeta()->getDbTable());
        // this assumes fields set_from_name has been invoked
        $primaryKeyFields = [];
        $unique_fields = [];
        $indexes = [];
        /** @var $field Field */
        /** @var $field ForeignKey */
        foreach ($model->getMeta()->localFields as $fname => $field) {
            $colName = $field->getColumnName();
            $type = $field->dbType($this->connection);
            // if we don't have a type skip
            if (empty($type)) {
                continue;
            }
            if ($field->primaryKey) {
                $primaryKeyFields[] = $model->getMeta()->primaryKey->getColumnName();
            } elseif ($field->isUnique()) {
                $unique_fields[] = $colName;
            } elseif ($field->dbIndex) {
                $indexes[] = $colName;
            }
            $tableDef->addColumn($colName, $type, $this->getDoctrineColumnOptions($field));
            if ($field->isRelation && $field->relation && $field->dbConstraint) {
                $relField = $field->getRelatedField();
                $tableDef->addForeignKeyConstraint(
                    $relField->scopeModel->getMeta()->getDbTable(),
                    [$field->getColumnName()],
                    [$relField->getColumnName()]
                );
            }
        }
        // create the primary key
        $tableDef->setPrimaryKey($primaryKeyFields);
        // add index constraint
        if (!empty($indexes)) {
            $tableDef->addIndex($indexes);
        }
        // add unique constraint
        if (!empty($unique_fields)) {
            $tableDef->addUniqueIndex($unique_fields);
        }
        $this->createTable($tableDef);
        // many to many
        /** @var $relationField ManyToManyField */
        foreach ($model->getMeta()->localManyToMany as $name => $relationField) {
            if ($relationField->manyToMany && $relationField->relation->through->getMeta()->autoCreated) {
                $this->createModel($relationField->relation->through);
            }
        }
    }
209762
public function newQuery() : QueryBuilder
    {
        $model = $this->container->make($this->getModelName());

        if (!$model instanceof Model) {
            throw new RepositoryException("Class {$this->model()} must be an instance of " . Model::class);
        }

        return $model->newQuery();
    }
264210
public function getQuery($model)
    {
        if ($this->query !== null) {
            return $this->query;
        }

        $this->query = $this->createQuery($model);
        if (!empty($this->activeQueries)) {
            $this->query->setActiveQueries($this->activeQueries);
        }

        $params = Yii::$app->request->queryParams;
        if (isset($params['query']) && !isset($params['ids'])) {
            $this->query->setActiveQueries($params['query']);
        }

        if (isset($params['search']['search-term']) && is_string($params['search']['search-term'])) {
            $this->query->quickSearchPhrase = $params['search']['search-term'];
        }

        if (isset($params['search']) && is_string($params['search'])) {
            $this->query->quickSearchPhrase = $params['search'];
        }

        // add extra authorization conditions
        if ($model->getBehavior('authorizer')) {
            $checkedRelations = $model->getCheckedRelations(Yii::$app->user->id, $this->modelClass . '.read');
            $this->query->authorized($model, $checkedRelations, Yii::$app->user->getIdentity());
        }

        if ($model instanceof ActiveSearchInterface) {
            $model->addConditions($this->query);
        }

        return $this->query;
    }
837414
public function fieldsOf($model)
    {
        $method = 'fields_get';

        $result = $this->call($model, $method, []);


        return $this->makeResponse($result);
    }
911030
public function newRawQuery()
    {
        $builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());

        // Once we have the query builders, we will set the model instances
        // so the builder can easily access any information it may need
        // from the model while it is constructing and executing various
        // queries against it.
        $builder->setModel($this)->with($this->with);
        return $builder;
    }
719215
protected function getModelInstanceQuery(Model $model)
    {
        $query = $model->query();

        $this->applyOrderingToModelInstanceQuery($query);
        $this->applyStartAndStopLimitsToQuery($query);

        return $query;
    }