Giter Club home page Giter Club logo

phalcon-collection's Introduction

Todo List

Register service:

    $di = new DI();
    $di->set('collection', function() {
        return new IzicaCollection();
    });

Usage:

    use Phalcon\Mvc\Controller;

    class IndexController extends Controller
    {
        public function indexAction()
        {
            $arCars = Cars::find();
            $jsonResponse = $this->collection->create($arCars)->groupBy('year')->toJson();
        }
    }

Functions

average

    $arr = [
        ['value' => 2, 'value2' => 4, 'value3' => 'foo'],
        ['value' => 6, 'value2' => 1, 'value3' => 'bar']
    ];
    $arResult = $this->collection->create($arr)->average();
    //Array
    //(
    //    [value] => 4
    //    [value2] => 2.5
    //)
    $arResult = $this->collection->create($arr)->average('value');
    //4
    $collection = $this->collection->create($arr)->average(['value']);
    //Array
    //(
    //    [value] => 4
    //)
    $collection = $this->collection->create([1, 5, 6 ,7])->average();
    //4.75

chunk

    $collection = $this->collection->create([1, 2, 3, 4, 5])->chunk(2);
    //[
    //    [1, 2], 
    //    [3, 4]
    //    [5]
    //]

groupBy

    $arResult = $this->collection->create($arr)->groupBy('value');
    $arResult = $this->collection->create($arr)->groupBy(function($arItem){
        return (int)$arItem['value'] * 2;
    });

keyBy

    $arResult = $this->collection->create($arr)->keyBy('id');
    $arResult = $this->collection->create($arr)->keyBy(function($arItem){
        return strtoupper($arItem['code']);
    });

has

    //has(key, value);
    $arr = ['value' => 'bar', 'value2' => 'foo'];
    $result = $this->collection->create($arr)->has('value'); // true
    $result = $this->collection->create($arr)->has('value', 'bar'); // true
    $result = $this->collection->create($arr)->has('value', 'xyz'); // false

toArray

transforms activeRecord to Array

    $arResult = $this->collection->create(Cars::find())->toArray()->groupBy('value'); // return result in Array Object after groupBy
    $arResult = $this->collection->create(Cars::find())->toArray(true) // return result in Array string instant, ~Cars::find()->toArray();

toJson

    $arResult = $this->collection->create($arr)->toJson()->groupBy('value'); // return result in JSON string after groupBy
    $arResult = $this->collection->create($arr)->toJson(true) // return result in JSON string instant

phalcon-collection's People

Contributors

izica avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.