Set CakePHP query

@param Query $query CakePHP query

@return CakePHP
514626
public function setQuery(Query $query)
    {
        $this->query = $query;
        $this->clonedQuery = clone $query;

        return $this;
    }
1166128
protected function setQuery($query)
    {
        $this->uriString = null;

        $query = ltrim($query, '?');

        if (strpos($query, '#') !== false) {
            throw new InvalidArgumentException(
                'Query string must not include a URI fragment.'
            );
        }

        $this->query = static::encode($query);

        return $this;
    }
347149
public function setLazyLoaded($query)
	{
		if( ! empty($this->getLazyLoad()))
		{
			$query = $query->load($this->getLazyLoad());
		}

		return $query;
	}
626633
public static function createActionStep(Query $query)
    {
        return new Action(
            $query->getResult()->getAction(),
            $query->getResult()->getParameters(),
            $query->getResult()->getContexts()
        );
    }
716708
protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();

        if (!is_array($keys)) {
            return parent::setKeysForSaveQuery($query);
        }

        foreach ($keys as $key) {
            $query->where($key, '=', $this->getKeyForSaveQuery($key));
        }

        return $query;
    }
166672
protected function setJoin(Query $query = null)
    {
        $query = $query ?: $this->query;

        $foreignKey = $this->relatedMap->getTable().'.'.$this->secondKey;

        $query->join($this->parentMap->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey);
    }
486031
public function setQuery($query = null)
    {

        try {

            // need to return << true
            $this->validator->verify($query, ['isNotNull', 'isAcceptLength']);

            if (false === $this->exact) {
                $this->query = ['query' => '%' . $query . '%'];
            }
            else {
                $this->query = ['query' => $query];
            }

        }
        catch(ExceptionFactory $e) {
            echo $e->getMessage();
        }

        return $this;
    }
1113273
public function setQuery($query)
    {
        $this->query = null;
        if (is_string($query)) {
            $this->query = ltrim($query, '?');
        }
        return $this;
    }
178205
public function execQuery(Query $query)
    {
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->connection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $query->isSingle());
        $resultDataType = $query->getResultDataType();

        foreach ($query as $sql) {
            $sqlResult = $sql->getResult();
            if ($resultDataType instanceof DataInterface) {
                $sqlResult->setDataType($resultDataType);
            }

            try {
                if ($this->error->hasError()) {
                    throw new \PDOException($this->error->getMessage());
                    break;
                }
                $sqlQuery = $sql->toString();
                $totalRows = 0;
                if ($sql->getCommand() == 'SELECT' && !$query->isSingle()) {
                    $template = '{SQL}';
                    $templateCounter = 'SELECT COUNT(*) totalRows FROM (%s) fakeTable';
                    $templateParams = array(
                        'SQL' => $sqlQuery,
                        'LIMIT' => $query->getLimit(),
                        'OFFSET' => $query->getOffset(),
                    );
                    if ($query->getLimit()) {
                        switch ($this->config->getType()) {
                            case 'firebird':
                                $template = 'SELECT FIRST {LIMIT} SKIP {OFFSET} * FROM ({SQL}) fakeTable';
                            break;
                            case 'oci':
                                $templateCounter = 'SELECT COUNT(*) "totalRows" FROM (%s) "fakeTable"';
                                $template = 'SELECT a.* FROM (SELECT b.*, rownum "[]" FROM '.
                                            '({SQL}) b WHERE rownum <= {LIMIT}) a WHERE "[]" > {OFFSET}';
                            break;
                            default:
                                $template = 'SELECT * FROM ({SQL}) fakeTable LIMIT {LIMIT} OFFSET {OFFSET}';
                            break;
                        }
                    }

                    $resultCounter = $this->connection->prepare(sprintf($templateCounter, $sqlQuery));
                    $resultCounter->execute();
                    $totalRows = $resultCounter->fetch();

                    $sqlResult->setTotalRows((int) @$totalRows['totalRows']);
                    foreach ($templateParams as $key => $val) {
                        $template = str_replace('{'.$key.'}', $val, $template);
                    }
                    $sqlQuery = $template;
                }

                $result = $this->connection->prepare($sqlQuery);
                $stmt = $result->execute();
                $sql->execute();

                if ($sql->isRead()) {
                    $columns = $this->_define_column_types($result);

                    $sqlResult->setColumnTypes($columns);
                    $sqlResult->setAffected(0);
                    $sqlResult->setStartRow($query->getOffset());
                    if (!$sqlResult->getTotalRows()) {
                        $sqlResult->setTotalRows($result->rowCount());
                    }

                    $rows = $result->fetchAll(\PDO::FETCH_ASSOC);

                    foreach ($rows as $row) {
                        $data = $this->_merge_values($sql->getResult()->getDataType(), $row, $columns);
                        $sqlResult->add($data);
                    }

                    $sqlResult->setTotalShows($sqlResult->length());
                    $sqlResult->setEnd(microtime(true));
                } elseif ($sql->isWrite() || $sql->isDDL()) {
                    $insertId = $this->connection->lastInsertId();
                    $affected = $result->rowCount();
                    $endTime = microtime(true);
                    $sqlResult->setEnd($endTime);

                    $columns = array(
                        'affected' => 'integer',
                        'seconds' => 'double',
                    );

                    $row = array(
                        'affected' => $affected,
                        'seconds' => $sqlResult->getSeconds(),
                    );

                    $sqlResult->setColumnTypes($columns);
                    $sqlResult->setAffected($affected);

                    $data = new Data();
                    $this->_merge_values($data, $row, $columns);
                    $sqlResult->add($data);
                    $sqlResult->setInsertId($insertId);
                }
            } catch (\PDOException $e) {
                $sql->execute(false);
                $this->error->setMessage($e->getMessage().". \n".$sql->toString());
                $sqlResult->setEnd(microtime(true));
            } catch (\Exception $e) {
                $sql->execute(false);
                $this->error->setMessage($e->getMessage().". \n".$sql->toString());
                $sqlResult->setEnd(microtime(true));
            }
        }
    }
689381
protected function formatQuery(Query $query)
    {
        // handle numeric where, from 2 to ['id'=>2]
        if (!$query->formatted() && $where = $query->get('where')) {
            $table = static::getFirstTable($query['table']);
            $atable  = static::getFirstTableAlias($query['table']);
            $model  = static::getQueryModel();
            $pk     = isset($model['primary_key']) ? $model['primary_key'] : null;
            $spk    = $atable . '.' . $pk;
            foreach ($where as $k=>$v) {
                if (is_numeric($k)) {
                    if (is_numeric($v)) {
                        if (!$pk) {
                            throw new Exception('pk is not set:' . $table);
                        }
                        $where[$k] = [$spk => $v];
                    } elseif (is_array($v)) {
                        foreach ($v as $k2=>$v2) {
                            if (is_numeric($k2) && is_numeric($v2)) {
                                if (!$pk) {
                                    throw new Exception('pk is not set:' . $table);
                                }
                                $where[$k][$k2] = [$spk => $v2];
                            }
                        }
                    }
                }
            }
            
            $query->setRaw('where', $where);
        }
        
        return $query;
    }
750943
public function setOptionsAndQuery($options = null, $query = '')
    {
        if ('' != $query) {
            $this->setQuery($query);
        }

        if (\is_int($options)) {
            $this->getQuery()->setSize($options);
        } elseif (\is_array($options)) {
            if (isset($options['limit'])) {
                $this->getQuery()->setSize($options['limit']);
                unset($options['limit']);
            }
            if (isset($options['explain'])) {
                $this->getQuery()->setExplain($options['explain']);
                unset($options['explain']);
            }
            $this->setOptions($options);
        }

        return $this;
    }
811520
public function setQuery($query)
	{
		if (is_array($query))
		{
			$this->vars = $query;
		}
		else
		{
			if (strpos($query, '&amp;') !== false)
			{
				$query = str_replace('&amp;', '&', $query);
			}

			parse_str($query, $this->vars);
		}

		$this->query = null;

		return $this;
	}
948935
protected function loadByQuery(Query $query)
    {
        $result = $this->doQuery($query);

        if ($result->getNumRows() == 0) {
            return null;
        }

        $this->fillProperties($result->fetch());

        return $this;
    }
538607
public function prepareExecuteAndFetchAll(Query $query)
    {
        $prepare = $this->prepareQuery($query);
        $execute = $this->executePrepared($prepare);

        return $this->fetchAllPrepared($prepare);
    }
1188224
public function query($query, array $params = [])
    {
        $this->query  = $query;
        $this->params = $params;
        $this->send();

        return $this;
    }
1209891
public function setQuery($query)
    {
        $this->dispatcher
            ->dispatch(DataGridEvents::onQueryReady, new QueryEvent($this->dataGrid->getName(), $query));
        
        $this->query = $query;
        
        return $this;
    }
655301
protected function setOperation($query, string $operation)
	{
		if(($query instanceof Subquery) === false)
		{
			$query = new Subquery($query);
		}

		$this->setOperations[] =
		[
			'query'     => $query,
			'operation' => $operation,
		];

		return $this;
	}