GithubHelp home page GithubHelp logo

jsondatabehavior's Introduction

JsonDataBehavior

Adds beforeSave and afterFind callbacks to model to json_encode and json_decode data on certain fields.

Setup

On every model that has a field that should be encoded you have to configure whitch fields will be Json.

For example, a User Model that have settings:

class User extends AppModel {
	
	public $actsAs		= array(
		'JsonData.JsonData' => [
			'fields' 	=> ['settings'],
		],
	);

	public $belongsTo	= array(
		'Group'
	);
}

On every associated Model you should also add the behavior without fields (unless it actualy have Json fields). This way, each time this associated model does a find and retrieves associated data, this data will also be decoded.

Example on Group Model:

class Group extends AppModel {
	
	public $actsAs		= array(
		'JsonData.JsonData',
	);

	public $hasMany		= array(
		'User',
	);
}

This way each time you call $groups = $this->Group->find('all', array('contain' => array('User'))));, $groups[{n}]['User']['settings'] will be an array. Also any associated Model with User and it's associated Models and so on will be decoded.

Methods

JsonData::decode(&$data, $modelName = null)

$data is an array with data retrieved from a Model::find().
$modelNameis the Model::alias. It's optional because the method is recursive and will check every $data to tell if it has a Json field (based on the associated models).

Does not return anything as $data is referenced.

jsondatabehavior's People

Contributors

bardiel avatar

Watchers

 avatar  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.