Cake\ORM\Table::_processFindOrCreate PHP Method

_processFindOrCreate() protected method

Performs the actual find and/or create of an entity based on the passed options.
protected _processFindOrCreate ( array | callable $search, callable $callback = null, array $options = [] ) : Cake\Datasource\EntityInterface
$search array | callable The criteria to find an existing record by, or a callable tha will customize the find query.
$callback callable A callback that will be invoked for newly created entities. This callback will be called *before* the entity is persisted.
$options array The options to use when saving.
return Cake\Datasource\EntityInterface An entity.
    protected function _processFindOrCreate($search, callable $callback = null, $options = [])
    {
        if (is_callable($search)) {
            $query = $this->find();
            $search($query);
        } elseif (is_array($search)) {
            $query = $this->find()->where($search);
        } elseif ($search instanceof Query) {
            $query = $search;
        } else {
            throw new InvalidArgumentException('Search criteria must be an array, callable or Query');
        }
        $row = $query->first();
        if ($row !== null) {
            return $row;
        }
        $entity = $this->newEntity();
        if ($options['defaults'] && is_array($search)) {
            $entity->set($search, ['guard' => false]);
        }
        if ($callback !== null) {
            $entity = $callback($entity) ?: $entity;
        }
        unset($options['defaults']);
        return $this->save($entity, $options) ?: $entity;
    }