Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

This is a model behavior built for CakePHP2 that allows you easily store flexible data sets by using a key/value type system.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Model
Octocat-spinner-32 README.textile
README.textile

KeyValue Behavior Plugin

Save random data in a key-value format.

Requirements

  • PHP 5.2
  • CakePHP 2.0+

Installation

[Manual]

  1. Download this: http://github.com/mdunham/KeyValue-Behavior/zipball/master
  2. Unzip that download.
  3. Copy the resulting folder to app/Plugin
  4. Rename the folder you just copied to KeyValue

[GIT Submodule]

In your app directory, type:

git submodule add git://github.com/mdunham/KeyValue-Behavior.git Plugin/KeyValue
git submodule init
git submodule update

[GIT Clone]

In your plugin directory, type:

git clone git://github.com/mdunham/KeyValue-Behavior.git KeyValue

Usage

Example of usage

Lets say I have a table for five fields:

id
created
key
value
user_id

Now lets say I wanted to store many different types of keys so there is not a locked down schema we might build a template form like so:

<?php
echo $this→Form→create(‘Measurement’);
echo $this→Form→input(‘weight’);
echo $this→Form→input(‘hips’);
echo $this→Form→input(‘waist’);
echo $this→Form→end(__(‘Submit’));
?>

And I want validation on those three inputs so my model might look like:

<?php

/**
* Measurement Model
*
* @property User $User
*/
class Measurement extends AppModel {

/**
* Tell this model how to behave
*
* @var array
*/
public $actsAs = array(‘KeyValue.KeyValue’);

/**
* Validation rules
*
* @var array
*/
public $validate = array(
‘weight’ => array(
‘numeric’ => array(
‘rule’ => array(‘numeric’),
‘message’ => ‘You must enter a weight’
)
),
‘waist’ => array(
‘numeric’ => array(
‘rule’ => array(‘numeric’),
‘message’ => ‘You must enter a value for waist’
)
),
‘hips’ => array(
‘numeric’ => array(
‘rule’ => array(‘numeric’),
‘message’ => ‘You must enter a value for hips’
)
),
);
?>

Options

  1. uniqueKeys: If this is true it run a deleteAll on the data to save minus the value
  2. fields: This is a single array that lets us know what field to use for the key and for the value

How it works

It will look over the data you tell it to save and it will pick out the fields that actual fields in your database like user_id for example, and it will generate a new record for every field that does not exist in your database table.

So if I said →save(array(‘Recipe’ => array(‘hips’ => ‘20’, ‘weight’ => ‘20’, ‘user_id’ => 2)); That would generate two records one for hips and one for weight but they would both contain the same user_id.

Todo

  • Better Readme
  • Unit Tests
  • Schema and Migrations

License

Copyright © 2011 Matthew Dunham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Something went wrong with that request. Please try again.