Callback to always return rows that have not been `trashed`.

@param \Cake\Event\Event $event Event.
@param \Cake\ORM\Query $query Query.
@param \ArrayObject $options Options.
@param bool $primary Primary or associated table being queries.
@return void
516880
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
    {
        $field = $this->getTrashField();
        $addCondition = true;

        $query->traverseExpressions(function ($expression) use (&$addCondition, $field) {
            if (!$addCondition) {
                return;
            }

            if ($expression instanceof IdentifierExpression
                && $expression->getIdentifier() === $field
            ) {
                $addCondition = false;

                return;
            }

            if (($expression instanceof Comparison || $expression instanceof BetweenExpression)
                && $expression->getField() === $field
            ) {
                $addCondition = false;
            }
        });

        if ($addCondition) {
            $query->andWhere($query->newExpr()->isNull($field));
        }
    }
276894
public function preSave(Event $event, Query $query, $id, array &$data) {
        $repo = $this->getRepository();
        $converters = $this->getConverters();

        foreach ($data as $key => $value) {
            if (empty($converters[$key])) {
                continue;
            }

            $converter = $converters[$key];

            // Exit if encoding should not happen
            if (!$converter['encode']) {
                continue;
            }

            switch ($converter['type']) {
                case self::SERIALIZE:   $value = $this->toSerialize($value, $converter); break;
                case self::JSON:        $value = $this->toJson($value, $converter); break;
                case self::BASE64:      $value = $this->toBase64($value, $converter); break;
                case self::CUSTOM:
                    if (method_exists($repo, $converter['encode'])) {
                        $value = call_user_func_array([$repo, $converter['encode']], [$value, $converter]);
                    }
                break;
            }

            $data[$key] = $value;
        }

        return true;
    }
398725
protected function add_primary_keys_to_where($query)
	{
		$primary_key = static::primary_key();
		foreach ($primary_key as $pk)
		{
			$query->where($pk, '=', $this->_original[$pk]);
		}
	}
643323
protected function setUpdateCriteria(
        Sql\SqlInterface $query, $primaryKey, $table
    ) {
        $key = "{$table}.{$primaryKey} = :id";
        $query->where([$key => [':id' => $this->entity->{$primaryKey}]]);
        return $query;
    }
953870
protected function insertRecord(Query $query): void
	{
		if($this->primaryKeyType === static::PRIMARY_KEY_TYPE_INCREMENTING)
		{
			$this->columns[$this->primaryKey] = $query->insertAndGetId($this->columns, $this->primaryKey);
		}
		else
		{
			switch($this->primaryKeyType)
			{
				case static::PRIMARY_KEY_TYPE_UUID:
					$this->columns[$this->primaryKey] = UUID::v4();
					break;
				case static::PRIMARY_KEY_TYPE_CUSTOM:
					$this->columns[$this->primaryKey] = $this->generatePrimaryKey();
					break;
			}

			$query->insert($this->columns);
		}
	}
1345329
private function _runQuery($query, $options)
    {
        $this->_testQueryStarts($query, '[select|show]');
        $statement = $this->_executeQuery($query, $options);
        if (is_object($statement) && $statement instanceof \PDOStatement) {
            return $statement;
        }
        else {
            $this->_error(FQDBException::INTERNAL_ASSERTION_FAIL, FQDBException::FQDB_CODE);
        }
    }
936434
public function delete($input)
    {
        $pk = $this->getPrimaryKey();

        if ($input instanceof $this->model) {
            $primary = $input->getPrimaryKey();
        } else {
            $primary = $input;
        }

        $query = $this->newDeleteQuery();
        $query->where("$pk = ?", $primary);
        $query->limit(1);

        $this->getDB()->execute($query);
    }
675346
public function queryAssoc($query, $options = array())
    {
        $statement = $this->_runQuery($query, $options);
        return $statement->fetch(\PDO::FETCH_ASSOC);
    }
979078
public function delete($query, array $options = []) {
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$params = $query->export($this);
			list($_path, $conditions) = $params['conditions'];
			$data = $query->data();

			if (!empty($data['rev'])) {
				$conditions['rev'] = $data['rev'];
			}
			$result = $this->connection->delete(
				"{$this->_config['database']}/{$_path}",
				$conditions
			);
			$result = json_decode($result);
			$result = (isset($result->ok) && $result->ok === true);

			if ($query->entity()) {
				$query->entity()->sync(null, [], ['dematerialize' => true]);
			}
			return $result;
		});
	}
309832
public function remove(array $query, array $options = array())
    {
        if ($this->eventManager->hasListeners(Events::preRemove)) {
            $this->eventManager->dispatchEvent(Events::preRemove, new EventArgs($this, $query, $options));
        }

        $result = $this->doRemove($query, $options);

        if ($this->eventManager->hasListeners(Events::postRemove)) {
            $this->eventManager->dispatchEvent(Events::postRemove, new EventArgs($this, $result));
        }

        return $result;
    }
331144
public function queriedUpdate($record, Query $query)
    {
        $identifier = $this->getIdentifier();

        if (isset($record[$identifier])) {
            throw new DataStoreException('Primary key is not allowed in record for queried update');
        }

        $forUpdateRecords = $this->query($query);
        $updatedIds = [];

        foreach ($forUpdateRecords as $forUpdateRecord) {
            try {
                $updatedRecord = $this->update(array_merge($record, [$identifier => $forUpdateRecord[$identifier]]));
                $updatedIds[] = $updatedRecord[$identifier];
            } catch (\Throwable $e) {
                // TODO: log failed queried updated record
                continue;
            }
        }

        return $updatedIds;
    }
98224
protected function performInsert(Builder $query, array $options = [])
	{
		if ($this->fireModelEvent('creating') === false) return false;

		// First we'll need to create a fresh query instance and touch the creation and
		// update timestamps on this model, which are maintained by us for developer
		// convenience. After, we will just continue saving these model instances.
		if ($this->timestamps && array_get($options, 'timestamps', true))
		{
			$this->updateTimestamps();
		}

		// If the model has an incrementing key, we can use the "insertGetId" method on
		// the query builder, which will give us back the final inserted ID for this
		// table from the database. Not all tables have to be incrementing though.
		$attributes = $this->attributes;

		if ($this->incrementing)
		{
			$this->insertAndSetId($query, $attributes);
		}

		// If the table is not incrementing we'll simply insert this attributes as they
		// are, as this attributes arrays must contain an "id" column already placed
		// there by the developer as the manually determined key for these models.
		else
		{
			$query->insert($attributes);
		}

		// We will go ahead and set the exists property to true, so that it is set when
		// the created event is fired, just in case the developer tries to update it
		// during the event. This will allow them to do so and run an update here.
		$this->exists = true;

		$this->fireModelEvent('created', false);

		return true;
	}
254515
protected function performUpdate( Builder $query, array $options = [] )
	{
		$dirty = $this->getDirty();

		if ( count( $dirty ) > 0 )
		{
			// If the updating event returns false, we will cancel the update operation so
			// developers can hook Validation systems into their models and cancel this
			// operation if the model does not pass validation. Otherwise, we update.
			if ( $this->fireModelEvent( 'updating' ) === false )
			{
				return false;
			}

			// First we need to create a fresh query instance and touch the creation and
			// update timestamp on the model which are maintained by us for developer
			// convenience. Then we will just continue saving the model instances.
			if ( $this->timestamps && Arr::get( $options, 'timestamps', true ) )
			{
				$this->updateTimestamps();
			}

			// Once we have run the update operation, we will fire the "updated" event for
			// this model instance. This will allow developers to hook into these after
			// models are updated, giving them a chance to do any special processing.
			$dirty = $this->getDirty();

			if ( count( $dirty ) > 0 )
			{
				$numRows = $this->setKeysForSaveQuery( $query )->update( $dirty );

				$this->fireModelEvent( 'updated', false );
			}
		}

		return true;
	}
894660
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) : void
    {
        // @todo this will be removed once saved searches table schema is adjusted
        $saved = Hash::get($data, 'content.saved', []);
        if (! empty($saved)) {
            $data['content']['latest'] = $saved;
        }
    }
1341771
public function beforeFind(Field $field, array $options, $primary)
    {
        if ($primary &&
            !Router::getRequest()->isAdmin() &&
            !empty($field->extra['from']['timestamp']) &&
            !empty($field->extra['to']['timestamp'])
        ) {
            $now = time();
            if ($field->extra['from']['timestamp'] > $now ||
                $now > $field->extra['to']['timestamp']
            ) {
                return null; // remove entity from result set
            }
        }

        return true;
    }
1368920
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) {
		$query->formatResults(function (CollectionInterface $results) {
			return $results->map(function ($row) {
				if (!$row instanceOf Entity) {
					return $row;
				}

				$this->decodeItems($row);
				return $row;
			});
		});
	}
759297
public function selectOne( $query, $bindings = [] )
	{
		$records = $this->select( $query, $bindings );

		return count( $records ) > 0 ? reset( $records ) : null;
	}