Git Product home page Git Product logo

yii-json-dataprovider's Introduction

yii-json-dataprovider

A yii dataprovider that extends CActiveDataProvider in order to retrieve json. Attributes and relations that are fetched, are recursively configurable. The dataprovier uses ModelToArrayConverter, a class that does the actual converting from models to an array representation. ModelToArrayConverter can be used stand-alone.

Update on 06/11/2013

  1. Extracting the work horse that does the converting to new class ModelToArrayConverter. This class is usable stand-alone, too.
  2. new function getArrayCountData on JSonActiveDataProvider to retrieve just the count information, but not data.
  3. New callback onAfterModelToArray on JSonActiveDataProvider for optional custom extracting of data to the final json per model.
  4. For convenience relations can be defined now directly with an attributes description (instead of wrapping them into another array with a key "attributes"

All changes do not break old behavior or usage!

Examples

  header('Content-Type: application/json');
  //same syntax like CActiveDataProvider
  $dataProvider = new JSonActiveDataProvider('Person'); 
  echo $dataProvider->getJsonData(); 
  //in order to get the raw array (before encoding) you can call
  $dataProvider->getArrayData(); 
  //limit the attributes to retrieve from Person
  $dataProvider = new JSonActiveDataProvider('Person', array('attributes' => array('id', 'name')));
  //only retrieve the "name" attribute of Person, same result like array('name')
  $dataProvider = new JSonActiveDataProvider('Person', array('attributes' => 'name'));
  //retrieve relations with all attribute, but no sub relations of "bankAccounts" or "address"
  $dataProvider = new JSonActiveDataProvider('Person', array(
    'attributes' => array('id', 'name'), 
    'relations' => array('bankAccounts', 'address')
  ));
  //same like above, but limit the attribite of the relations and retrieve relation "bank" of each "bankAccounts"
  $dataProvider = new JSonActiveDataProvider('Person', array(
    'attributes' => array('id', 'name'), 
    'relations' => array(
      'bankAccounts' => array(
        'attributes' => array('id', 'account_number'), 
        'relations' => array(
          'bank' => array(
            'attributes' => array('id', 'name')
          )
        )
      )
    )
  ));
  //same like above, but with convenient attribute definition of relations
  $dataProvider = new JSonActiveDataProvider('Person', array(
    'attributes' => array('id', 'name'), 
    'relations' => array(
      'bankAccounts' => array(
        'attributes' => array('id', 'account_number'), 
        'relations' => array(
          'bank' => array('id', 'name')
        )
      )
    )
  ));
  //replacing "id" by "i" and "name" by "n" in all results
  $dataProvider = new JSonActiveDataProvider('Person', array(
    'attributeAliases' => array('id' => 'i', 'name' => 'n'),
    'attributes' => array('id', 'name'), 
    'relations' => array(
      'bankAccounts' => array(
        'attributes' => array('id', 'account_number'), 
        'relations' => array(
          'bank' => array(
            'attributes' => array('id', 'name')
          )
        )
      )
    )
  ));
  //using a callback to manipulate the json outcome manually
  //in this example we us a closure, but any callback as defined here is fine: 
  //http://php.net/manual/en/language.types.callable.php
  
  $callback = function ($model, &$attributeArray, &$relationArray)
	{
		if ($model instanceof Person)
		{
			$attributeArray['bar'] = 'foo';
		}
	};
    
  $dataProvider = new JSonActiveDataProvider('Person', array(
    'onAfterModelToArray' => $callback,
  ));
  //converting a custom model array without JSonActiveDataProvider
  $array = ModelToArrayConverter::instance($myModels)->convert();
  
  //with optional configuration whhich attributes to be converted (third parameter accepts relation config)
  $array = ModelToArrayConverter::instance($myModels, array('id', 'name'))->convert();

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.