All Projects → yii2mod → yii2-array-query

yii2mod / yii2-array-query

Licence: MIT license
Yii2 component that allows for searching/filtering the elements of an array.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-array-query

yii2-translatable
Translatable behavior aggregates logic of linking translations to the primary model
Stars: ✭ 15 (-55.88%)
Mutual labels:  yii2, yii2-extension
yii2-rollbar
Rollbar for Yii2
Stars: ✭ 36 (+5.88%)
Mutual labels:  yii2, yii2-extension
yii2-stat
Yii2 Multi Web Statistic Module (yametrika, google-analytic, own db-counter)
Stars: ✭ 18 (-47.06%)
Mutual labels:  yii2, yii2-extension
yii2-lets-talk
With this extension you can open chat with someone in popular messengers using the link on your website.
Stars: ✭ 15 (-55.88%)
Mutual labels:  yii2, yii2-extension
yii2-pgsql
Improved PostgreSQL schemas for Yii2
Stars: ✭ 34 (+0%)
Mutual labels:  yii2, yii2-extension
content
Content management system for Yii2
Stars: ✭ 54 (+58.82%)
Mutual labels:  yii2, yii2-extension
yii2-bankcard-info
银行卡卡号分析(Yii2扩展)
Stars: ✭ 15 (-55.88%)
Mutual labels:  yii2, yii2-extension
Yii2 Enhanced Gii
Enhanced Yii2 Gii (generator) that generates related Models & CRUD
Stars: ✭ 183 (+438.24%)
Mutual labels:  yii2, yii2-extension
behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (-47.06%)
Mutual labels:  yii2, yii2-extension
yii2-cashier
Yii2 Cashier provides an interface to Stripe's subscription billing services.
Stars: ✭ 43 (+26.47%)
Mutual labels:  yii2, yii2-extension
install
basic script for project installation
Stars: ✭ 17 (-50%)
Mutual labels:  yii2, yii2-extension
yii2-vote
Provides voting for any model 👍 👎
Stars: ✭ 70 (+105.88%)
Mutual labels:  yii2, yii2-extension
Yii2 Translate Manager
Translation Manager
Stars: ✭ 221 (+550%)
Mutual labels:  yii2, yii2-extension
yii2-mailqueue
Yii2 mail queue component for yii2-swiftmailer.
Stars: ✭ 15 (-55.88%)
Mutual labels:  yii2, yii2-extension
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+452.94%)
Mutual labels:  yii2, yii2-extension
yii2-newsletter
Module for saving user contacts from newsletter form to database
Stars: ✭ 17 (-50%)
Mutual labels:  yii2, yii2-extension
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+376.47%)
Mutual labels:  yii2, yii2-extension
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+400%)
Mutual labels:  yii2, yii2-extension
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (-8.82%)
Mutual labels:  yii2, yii2-extension
yii2-jwt-tools
An easy way to configure JWT authentication and validation on Yii Framework 2 Projects
Stars: ✭ 22 (-35.29%)
Mutual labels:  yii2, yii2-extension

Yii2 ArrayQuery Component


Allows searching/filtering of an array. This component is very useful when displaying array data in GridViews with an ArrayDataProvider.

Latest Stable Version Total Downloads License Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-array-query "*"

or add

"yii2mod/yii2-array-query": "*"

to the require section of your composer.json.

Querying Data

You may execute complex query on the array data using [[\yii2mod\query\ArrayQuery]] class. This class works similar to regular [[\yii\db\Query]] and uses same syntax. For example:

$data = [
    [
        'id' => 1,
        'username' => 'admin',
        'email' => '[email protected]'
    ],
    [
        'id' => 2,
        'username' => 'test',
        'email' => '[email protected]'
    ],
];

$query = new ArrayQuery();
$query->from($data);
$query->where(['username' => 'admin']);

$rows = $query->all();

Using with ArrayDataProvider

You may perform filtering using [[\yii2mod\query\ArrayQuery]] class. For example:

<?php

// Some search model
    
/**
 * Setup search function for filtering and sorting.
 * @param $params
 * @return ArrayDataProvider
 */
public function search($params)
{
    $models = User::find()->asArray()->all();

    $query = new ArrayQuery();
    $query->from($models);

    // load the search form data and validate
    if ($this->load($params) && $this->validate()) {
        // adjust the query by adding the filters
        $query->andFilterWhere(['id' => $this->id]);
        $query->andFilterWhere(['status' => $this->status]);
        $query->andFilterWhere(['like', 'username', $this->username]);
        $query->andFilterWhere(['like', 'email', $this->email]);
    }
    
    // prepare the ArrayDataProvider
    return new ArrayDataProvider([
        'allModels' => $query->indexBy('id')->all(),
        'sort' => [
            'attributes' => ['id', 'username', 'email', 'status'],
        ],
        'pagination' => [
            'pageSize' => 10
        ],
    ]);
}

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].