Find a single entity by specific field values

@param  array  $wheres An array of where conditions in the format:
['column' => 'value'] or
['column', 'operator', 'value']
@param  array  $options
@return AbstractEntity|bool
541509
public function findBy(array $wheres, array $options = [])
    {
        $query = $this->getSqlObject()->select();

        $this->setColumns($query, $options);

        $wheres = $this->addJoins($query, $wheres, $options);

        $this->addWheres($query, $wheres, $options);

        if (Arr::get($options, 'order')) {
            $this->setOrder($query, $options['order']);
        }

        return $this->executeAndGetResultsAsEntity($query);
    }
797896
public function select(array $where = [], array $nosql_options = [])
    {
        // Set statement
        $query = $this->getSelect($where);
        $statement = $this->getInstance()->prepare($query);

        // Bind where values
        foreach ($where as $key => $value) {
            $statement->bindValue(":where_$key", $value, $this->isPdoInt($value));
        }

        // Execute operation
        $statement->execute();

        // Return object
        return $statement->fetchAll(PDO::FETCH_OBJ);
    }
185544
public function select($tbl, $where, $options = array()){
		return $this->crud->select($tbl, $where, $options);
	}
600648
protected function _find($id, $options = [])
    {
        $defaults = [
            'query'        => [],
            'fetchOptions' => []
        ];
        $options += $defaults;

        $fetchOptions = $options['fetchOptions'];
        unset($options['fetchOptions']);

        if ($this->link() !== static::LINK_KEY) {
            throw new ORMException("This relation is not based on a foreign key.");
        }
        $to = $this->to();
        $schema = $to::definition();

        if (!$id) {
            return $to::create([], ['type' => 'set']);
        }

        $ids = is_array($id) ? $id : [$id];
        $key = $schema->key();
        $column = $schema->column($key);

        foreach ($ids as $i => $value) {
            $ids[$i] = $schema->convert('cast', $column['type'], $value, $column);
        }

        if (count($ids) === 1) {
            $ids = reset($ids);
        }
        $conditions = [$this->keys('to') => $ids];
        if ($this->conditions()) {
            $conditions = [
                ':and()' => [
                    [$this->keys('to') => $ids],
                    $this->conditions()
                ]
            ];
        }
        $query = Set::extend($options['query'], ['conditions' => $conditions]);
        return $to::all($query, $fetchOptions);
    }
70965
public function findAll(string $from, $wheres = 1, $select = '*', array $options = [])
    {
        $options['select'] = $this->qns($select ?: '*');
        $options['from'] = $this->qn($from);

        list($where, $bindings) = $this->handleWheres($wheres);

        $options['where'] = $where;

        if (!isset($options['limit'])) {
            $options['limit'] = 1000;
        }

        $statement = $this->compileSelect($options);

        if (isset($options['dumpSql'])) {
            return [$statement, $bindings];
        }

        $indexKey = $options['indexKey'] ?? null;

        if ($class = $options['class'] ?? null) {
            return $this->fetchObjects($statement, $bindings, $class, $indexKey);
        }

        $method = 'fetchAssocs';

        if (isset($options['fetchType'])) {
            if ($options['fetchType'] === 'column') {
                // for get columns, indexKey is column number.
                $method = 'fetchColumns';
            } elseif ($options['fetchType'] === 'value') {
                $method = 'fetchValues';
            }
        }

        return $this->$method($statement, $bindings, $indexKey);
    }
1345803
public function find($entityName, $where = [], $joinConditions = [])
    {
        return $this->getRepository($entityName)->find($where, 'u', false, $joinConditions);
    }
1222586
public function select($options = [])
    {
        $sql = str_replace(
            ['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
            [
                $this->parseTable($options['table'], $options),
                $this->parseDistinct($options['distinct']),
                $this->parseField($options['field'], $options),
                $this->parseJoin($options['join'], $options),
                $this->parseWhere($options['where'], $options),
                $this->parseGroup($options['group']),
                $this->parseHaving($options['having']),
                $this->parseOrder($options['order'], $options),
                $this->parseLimit($options['limit']),
                $this->parseUnion($options['union']),
                $this->parseLock($options['lock']),
                $this->parseComment($options['comment']),
                $this->parseForce($options['force']),
            ], $this->selectSql);
        return $sql;
    }
97896
public function findWhere(array $where, $columns = ['*'])
    {
        $this->applyCriteria();
        $this->applyScope();

        $this->applyConditions($where);

        $model = $this->model->get($columns);
        $this->resetModel();

        return $this->parserResult($model);
    }
1202319
public function findAllBy(array $conditions = array())
    {
        $sql = sprintf('SELECT * FROM %s', $this->getTableName());
        $whereConditions = $this->assembleEquality($conditions);
        $where = sprintf('WHERE %s', implode(' AND ', $whereConditions));
        $sql .= " $where";

        return $this->selectQuery($sql, $conditions);
    }
494587
public static function findByIds($className, array $ids, array $options): ResultInterface
    {
        list($tableName, , $columnId) = self::getTable($className);
        $instance = self::getInstance($className);

        $query  = Query::table($tableName)->className($className)->whereIn($columnId, $ids)->selectInstance($instance);
        $query  = self::applyOptions($query, $options);
        $fields = self::getFieldsFromOptions($options);

        return $query->get($fields);
    }
36293
public function delete(string $from, $wheres, array $options = [])
    {
        if (!$wheres) {
            throw new \InvalidArgumentException('Safety considerations, where conditions can not be empty');
        }

        list($where, $bindings) = DBHelper::handleConditions($wheres, $this);

        $options['from'] = $this->qn($from);
        $options['where'] = $where;

        $statement = $this->compileDelete($options);

        if (isset($options['returnSql'])) {
            return [$statement, $bindings];
        }

        return $this->fetchAffected($statement, $bindings);
    }
915103
public static function find($id = null, array $options = array())
	{
		// deal with null valued PK's
		if (is_null($id))
		{
			// if no options are present, simply return null. a PK with a null value can exist
			return func_num_args() === 2 ? static::query($options) : null;
		}

		// Return all that match $options array
		elseif ($id === 'all')
		{
			return static::query($options)->get();
		}

		// Return first or last row that matches $options array
		elseif ($id === 'first' or $id === 'last')
		{
			$query = static::query($options);

			foreach(static::primary_key() as $pk)
			{
				$query->order_by($pk, $id == 'first' ? 'ASC' : 'DESC');
			}

			return $query->get_one();
		}

		// Return specific request row by ID
		else
		{
			$cache_pk = $where = array();
			$id = (array) $id;
			foreach (static::primary_key() as $pk)
			{
				$where[] = array($pk, '=', current($id));
				$cache_pk[$pk] = current($id);
				next($id);
			}

			if (array_key_exists(get_called_class(), static::$_cached_objects)
			    and array_key_exists(static::implode_pk($cache_pk), static::$_cached_objects[get_called_class()])
			    and (! isset($options['from_cache']) or $options['from_cache'] == true))
			{
				return static::$_cached_objects[get_called_class()][static::implode_pk($cache_pk)];
			}

			array_key_exists('where', $options) and $where = array_merge($options['where'], $where);
			$options['where'] = $where;
			return static::query($options)->get_one();
		}
	}
231791
public function find($type = 'all', $options = [])
    {
        $query = parent::find($type, $options);

        //Unserializes the `extra` field.
        $query->formatResults(function (ResultSet $results) {
            return $results->map(function (Token $token) {
                if ($token->extra) {
                    $token->extra = @unserialize($token->extra);
                }

                return $token;
            });
        });

        return $query;
    }
1009499
public function find($options=null, $format=false)
    {
        if (isset($options)) {
            if (is_numeric($options)) {
                return $this->findById($options);
            } else {
                $this->getQuery()->set($this->formatQueryOptions($options));
            }
        }
        
        $model = static::getQueryModel();
        
        $this->getQuery()->type('select')->set('limit', 1);
        
        $data = $this->query();
        
        if ($data && count($data) > 0) {
            $data = $data[0];
            if ($format) {
                $data = static::formatOutput($data, $model);
            }
        } else {
            $data = null;
        }
        
        return $data;
    }
36828
public static function find($fields = '*')
    {
        $entity = new static();
        if ($fields == '*') {
            $fields = $entity->getTableName() .'.*';
        }
        Entity\Manager::getInstance()->get($entity)->refreshRelations();
        $select = new \Slick\Orm\Sql\Select($entity, $fields);
        return $select;
    }
98973
public function getResults(array $options = []) : ?SavedSearch
    {
        $this->setContainerId($options['entity']);

        $table = TableRegistry::get('Search.SavedSearches');

        /** @var \Search\Model\Entity\SavedSearch|null */
        $savedSearch = $table->find()
            ->where(['id' => $this->widget->get('widget_id')])
            ->enableHydration(true)
            ->first();

        if (null === $savedSearch) {
            $this->errors[] = 'No data found for this entity';

            return null;
        }

        /** @var \Cake\Datasource\RepositoryInterface */
        $table = TableRegistry::get($savedSearch->get('model'));

        $search = new Search($table, $options['user']);
        // keeps backward compatibility
        $entity = $search->reset($savedSearch);
        if (null === $entity) {
            $this->errors[] = 'Failed to reset entity';

            return null;
        }
        $content = $entity->get('content');
        $content['saved'] = Validator::validateData($table, $content['saved'], $options['user']);
        $entity->set('content', $content);

        $this->options['fields'] = Utility::instance()->getSearchableFields($table, $options['user']);
        $this->options['associationLabels'] = Utility::instance()->getAssociationLabels($table);

        $this->data = $entity;

        return $this->data;
    }
431680
protected function getFreshDistinct($wheres)
    {
        $column = isset($this->columns[0]) ? $this->columns[0] : '_id';

        // Execute distinct
        if ($wheres) {
            $result = $this->collection->distinct($column, $wheres);
        } else {
            $result = $this->collection->distinct($column);
        }

        return $result;
    }