Initializes base query for model $m.

@param Model $m

@return \atk4\dsql\Query
510658
public function initQuery($m)
    {
        $d = $m->persistence_data['dsql'] = $this->dsql();

        if ($m->table) {
            if (isset($m->table_alias)) {
                $d->table($m->table, $m->table_alias);
            } else {
                $d->table($m->table);
            }
        }

        return $d;
    }
1223159
public function fromModel($model, string $name, string $key = null): self
    {
        /* @var $model Model */
        $model = is_object($model) ? $model : new $model();
        $key = $key ?? $model->getModel()->getKeyName();

        return $this->setFromEloquent($model, $name, $key);
    }
510975
protected function initializeModelRepository()
    {
        /** @var ModelRepositoryFactoryInterface $factory */
        $factory = app(ModelRepositoryFactoryInterface::class);

        $this->modelRepository = $factory->make($this->modelInformation->modelClass());

        $this->applyRepositoryContext();

        return $this;
    }
738583
public function tryLoad(Model $m, $id)
    {
        if (!$m->id_field) {
            throw new Exception(['Unable to load field by "id" when Model->id_field is not defined.', 'id'=>$id]);
        }

        $load = $m->action('select');
        $load->where($m->getField($m->id_field), $id);
        $load->limit(1);

        // execute action
        try {
            $data = $this->typecastLoadRow($m, $load->getRow());
        } catch (\PDOException $e) {
            throw new Exception([
                'Unable to load due to query error',
                'query'      => $load->getDebugQuery(false),
                'model'      => $m,
                'conditions' => $m->conditions,
            ], null, $e);
        }

        if (!$data) {
            return;
        }

        if (!isset($data[$m->id_field]) || is_null($data[$m->id_field])) {
            throw new Exception([
                'Model uses "id_field" but it wasn\'t available in the database',
                'model'       => $m,
                'id_field'    => $m->id_field,
                'id'          => $id,
                'data'        => $data,
            ]);
        }

        $m->id = $data[$m->id_field];

        return $data;
    }
1281535
protected function initialiseQuery()
    {
        // Join on base table and let lazy loading join subtables
        $baseClass = DataObject::getSchema()->baseDataClass($this->dataClass());
        if (!$baseClass) {
            throw new InvalidArgumentException("DataQuery::create() Can't find data classes for '{$this->dataClass}'");
        }

        // Build our intial query
        $this->query = new SQLSelect(array());
        $this->query->setDistinct(true);

        if ($sort = singleton($this->dataClass)->config()->get('default_sort')) {
            $this->sort($sort);
        }

        $baseTable = DataObject::getSchema()->tableName($baseClass);
        $this->query->setFrom("\"{$baseTable}\"");

        $obj = Injector::inst()->get($baseClass);
        $obj->extend('augmentDataQueryCreation', $this->query, $this);
    }
1143419
public function insert(Model $m, $data, $table = null)
    {
        if (!isset($table)) {
            $table = $m->table;
        }

        $data = $this->typecastSaveRow($m, $data);

        $id = $this->generateNewID($m, $table);
        $data[$m->id_field] = $id;
        $this->data[$table][$id] = $data;

        return $id;
    }
881428
public function model(Model $model)
    {
        // Check if the model is searchable before setting the query builder model
        $traits = class_uses_recursive(get_class($model));

        if (!isset($traits[Searchable::class])) {
            throw new InvalidArgumentException(get_class($model).' does not use the searchable trait');
        }

        $this->model = $model;

        return $this;
    }
1135699
public function getQueryModel($model=null, $self=true)
    {
        if (!$model) {
            $model = static::getFirstTable();
        }
        
        if ($model && is_string($model)) {
            $model = preg_replace('/^' . $this->getTablePrefix() . '/', '', $model, 1);
            return static::getTableInfo($model);
        } else {
            return $model ? : ($self ? $this : null);
        }
    }
1319030
protected function buildModel($model)
    {
        try {
            $query = $this->resolveQueryBuilderFromModel($model);
        } catch (InvalidArgumentException $e) {
            $query = $model;
        }

        if ($this->paginate === true && \method_exists($query, 'paginate')) {
            $query = $query->paginate($this->perPage, ['*'], $this->pageName);
        } elseif ($this->isQueryBuilder($query)) {
            $query = $query->get();
        }

        return $query;
    }
1196607
private function buildQuery(Model $model = null)
    {
        $query = $this->model->collections();

        $query = $query->whereCollectionName($this->collectionName);

        if ($model) {
            $query = $query->where('item_id', $model->id)
                           ->where('item_type', get_class($model));
        }

        if (!$model && $this->itemType) {
            $query = $query->where('item_type', '=', $this->itemType);
        }

        return $query;
    }
1364742
public function setModel($m)
    {
        if (is_string($m)) {
            $m = 'Model_'.$m;
        }
        $this->model = $this->add($m);

        return $this->model;
    }
1320975
public function initWithModelAndAdapter(Model $m, $adapter)
    {
        $this->adapter = $adapter;

        $this->model = $m;
        $this->model->printModel();

        $this->fm = Model::loadFunctionMap();

        $this->initialize();

        if (null !== $this->adapter) {
            try {
                $this->loadPolicy();
            } catch (\Exception $e) {
                // error intentionally ignored
            }
        }
    }
226775
public function setModel(Model $model)
    {
        parent::setModel($model);
        $this->setHtmlAttribute('data-value', $this->getModelValue());

        return $this;
    }
1193229
public function setModel(Model $model)
    {
        $this->model = $model;
        $append = $this->getAppends();

        if ($append instanceof WithModelInterface) {
            $append->setModel($model);
        }

        return $this;
    }
73996
protected function baseQuery()
    {
        if (empty($this->baseQuery) && $this->parent->getData()) {
            $pk    = $this->parent->getPk();
            $table = $this->pivot->getTable();

            $this->query
                ->join($table . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())
                ->where('pivot.' . $this->localKey, $this->parent->$pk);
            $this->baseQuery = true;
        }
    }
1338617
public static function createFromModel(Model $model)
    {
        $table = $model->getTable();

        $dc = new static($table);

        $dc->strTable = $model->getTable();
        $dc->activeRecord = $model;
        $dc->intId = $model->id;

        return $dc;
    }
297216
public function tryLoadAny(Model $m)
    {
        $load = $m->action('select');
        $load->limit(1);

        // execute action
        try {
            $data = $this->typecastLoadRow($m, $load->getRow());
        } catch (\PDOException $e) {
            throw new Exception([
                'Unable to load due to query error',
                'query'      => $load->getDebugQuery(false),
                'model'      => $m,
                'conditions' => $m->conditions,
            ], null, $e);
        }

        if (!$data) {
            return;
        }

        if ($m->id_field) {

            // If id_field is not set, model will be read-only
            if (isset($data[$m->id_field])) {
                $m->id = $data[$m->id_field];
            } else {
                throw new Exception([
                    'Model uses "id_field" but it was not available in the database',
                    'model'       => $m,
                    'id_field'    => $m->id_field,
                    'data'        => $data,
                ]);
            }
        }

        return $data;
    }