Class luya\admin\file\Query

Inheritanceluya\admin\file\Query » yii\base\BaseObject
Implementsyii\base\Configurable
Uses Traitsluya\admin\storage\QueryTrait
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/file/Query.php

Storage Files Querying.

See the {{\luya\admin\storage\QueryTrait}} for more informations.

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
all() Find all elementes based on the where filter. luya\admin\storage\QueryTrait
andWhere() Add another where statement to the existing, this is the case when using compare operators, as then only one where definition can bet set. luya\admin\storage\QueryTrait
bind() Bind given values into the objects for a given id. luya\admin\storage\QueryTrait
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
count() Get the count of items luya\admin\storage\QueryTrait
createItem() luya\admin\file\Query
createIteratorObject() luya\admin\file\Query
findOne() FindOne with the specific ID. luya\admin\storage\QueryTrait
getDataProvider() luya\admin\file\Query
getItemDataProvider() luya\admin\file\Query
getStorage() Singleton behavior for storage component getter. luya\admin\file\Query
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
limit() Set a limition for the amount of results. luya\admin\storage\QueryTrait
offset() Define offset start for the rows, if you defined offset to be 5 and you have 11 rows, the first 5 rows will be skiped. This is commonly used to make pagination function in combination with the limit() function. luya\admin\storage\QueryTrait
one() Find One based on the where condition. luya\admin\storage\QueryTrait
orderBy() Order the query by one or multiple fields asc or desc. luya\admin\storage\QueryTrait
where() Query where similar behavior of filtering items. luya\admin\storage\QueryTrait

Property Details

Hide inherited properties

$dataProvider public read-only property
public void getDataProvider ( )
$storage public property

The storage component

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
all() public method

Defined in: luya\admin\storage\QueryTrait::all()

Find all elementes based on the where filter.

public luya\admin\storage\IteratorAbstract all ( )

                public function all()
{
    return $this->createIteratorObject($this->filter());
}

            
andWhere() public method

Defined in: luya\admin\storage\QueryTrait::andWhere()

Add another where statement to the existing, this is the case when using compare operators, as then only one where definition can bet set.

See {{luya\admin\storage\QueryTrait::where()}}

public luya\admin\storage\QueryTrait andWhere ( array $args )
$args array

The where definition can be either an key-value pairing or a condition representen as array.

                public function andWhere(array $args)
{
    return $this->where($args);
}

            
bind() public method (available since version 1.1.1)

Defined in: luya\admin\storage\QueryTrait::bind()

Bind given values into the objects for a given id.

(new Query())->find()->where(['in', 'id', [1,2,3])->bind([1 => ['caption' => 'barfoo'])->all();
public luya\admin\storage\QueryTrait bind ( array $values )
$values array

                public function bind(array $values)
{
    if (!empty($values)) {
        $this->_binds = $values;
    }
    return $this;
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
count() public method

Defined in: luya\admin\storage\QueryTrait::count()

Get the count of items

public integer count ( )
return integer

Amount of filtere data.

                public function count()
{
    return count($this->filter());
}

            
createItem() public method

public void createItem ( array $itemArray )
$itemArray

                public function createItem(array $itemArray)
{
    return Item::create($itemArray);
}

            
createIteratorObject() public method

public void createIteratorObject ( array $data )
$data

                public function createIteratorObject(array $data)
{
    return Yii::createObject(['class' => Iterator::class, 'data' => $data]);
}

            
findOne() public method

Defined in: luya\admin\storage\QueryTrait::findOne()

FindOne with the specific ID.

public luya\admin\image\Item|luya\admin\file\Item|luya\admin\folder\Item findOne ( $id )
$id integer

The specific item id

                public function findOne($id)
{
    return ($itemArray = $this->getItemDataProvider($id)) ? $this->createItem($itemArray) : false;
}

            
getDataProvider() public method

public void getDataProvider ( )

                public function getDataProvider()
{
    return $this->storage->filesArray;
}

            
getItemDataProvider() public method

public void getItemDataProvider ( $id )
$id

                public function getItemDataProvider($id)
{
    return $this->storage->getFilesArrayItem($id);
}

            
getStorage() public method

Singleton behavior for storage component getter.

public luya\admin\storage\BaseFileSystemStorage getStorage ( )

                public function getStorage()
{
    if ($this->_storage === null) {
        $this->_storage = Yii::$app->storage;
    }
    return $this->_storage;
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
limit() public method

Defined in: luya\admin\storage\QueryTrait::limit()

Set a limition for the amount of results.

public luya\admin\storage\QueryTrait limit ( $count )
$count integer

The number of rows to return

                public function limit($count)
{
    if (is_numeric($count)) {
        $this->_limit = $count;
    }
    return $this;
}

            
offset() public method

Defined in: luya\admin\storage\QueryTrait::offset()

Define offset start for the rows, if you defined offset to be 5 and you have 11 rows, the first 5 rows will be skiped. This is commonly used to make pagination function in combination with the limit() function.

public luya\admin\storage\QueryTrait offset ( $offset )
$offset integer

Defines the amount of offset start position.

                public function offset($offset)
{
    if (is_numeric($offset)) {
        $this->_offset = $offset;
    }
    return $this;
}

            
one() public method

Defined in: luya\admin\storage\QueryTrait::one()

Find One based on the where condition.

If there are several items, it just takes the first one and does not throw an exception.

public luya\admin\image\Item|luya\admin\file\Item|luya\admin\folder\Item one ( )

                public function one()
{
    $data = $this->filter();
    return (count($data) !== 0) ? $this->createItem(array_values($data)[0]) : false;
}

            
orderBy() public method (available since version 4.0.0)

Defined in: luya\admin\storage\QueryTrait::orderBy()

Order the query by one or multiple fields asc or desc.

Use following PHP constants for directions:

  • SORT_ASC: 1..10, A..Z
  • SORT_DESC: 10..1, Z..A

Example using orderBy:

$query = new Query()->orderBy(['id => SORT_ASC])->all();

In rare cases you like to sort for certain existing order structure, for example when an explicit order is given from an user input, then you can provide an array of that value. The limitation for this order behavior is that only elements in the list will be taken, other elements will be removed from the result array. This means if an id is not present in that array of orderding by id, this will be removed.

Example usage:

(new Query())->where(['in', 'id', [1,2,3]])->orderBy(['id' => [3,2,1]])->all();

The above example will return those elements in the order of 3,2,1.

Example usage which will remove elements:

(new Query())->where(['in', 'id', [1,2,3]])->orderBy(['id' => [2,1]])->all();

The above example will return only the order elements 2,1 and element with id 3 is gone

public luya\admin\storage\QueryTrait orderBy ( array $order )
$order array

An array with fields to sort where key is the field and value the direction.

                public function orderBy(array $order)
{
    $orderBy = ['keys' => [], 'directions' => [], 'ids' => []];
    foreach ($order as $key => $direction) {
        $orderBy['keys'][] = $key;
        $orderBy['directions'][] = $direction;
        if (is_array($direction)) {
            $orderBy['ids'] = $direction;
        }
    }
    $this->_order = $orderBy;
    return $this;
}

            
where() public method

Defined in: luya\admin\storage\QueryTrait::where()

Query where similar behavior of filtering items.

Operator Filtering:

where(['operator', 'field', 'value']);

Available Operators:

  • < expression where field is smaller then value.
  • > expression where field is bigger then value.
  • = expression where field is equal value.
  • <= expression where field is small or equal then value.
  • >= expression where field is bigger or equal then value.
  • == expression where field is equal to the value and even the type must be equal.
  • in expression where an value array can be passed to get all values from this field type e.g. ['in', 'id', [1,3,4]].

Only one operator speific argument can be provided, to chain another expression use the andWhere() method.

Multi Dimension Filtering:

The most common case for filtering items is the equal expression combined with add statements.

For example the following expression

where(['=', 'id', 0])->andWhere(['=', 'name', 'footer']);

is equal to the short form multi deimnsion filtering expression

where(['id' => 0, 'name' => 'footer']);

Its not possibile to make where conditions on the same column:

where(['>', 'id', 1])->andWHere(['<', 'id', 3]);

This will only appaend the first condition where id is bigger then 1 and ignore the second one

public luya\admin\storage\QueryTrait where ( array $args )
$args array

The where definition can be either an key-value pairing or a condition representen as array.

throws luya\Exception

                public function where(array $args)
{
    foreach ($args as $key => $value) {
        if (in_array($value, $this->_whereOperators, true)) {
            if (count($args) !== 3) {
                throw new Exception("Wrong where condition. Condition needs an operator and two operands.");
            }
            $this->_where[] = ['op' => $args[0], 'field' => $args[1], 'value' => $args[2]];
            break;
        } else {
            $this->_where[] = ['op' => '=', 'field' => $key, 'value' => $value];
        }
    }
    return $this;
}