Download the PHP package carpehora/extrapropertiesbehavior without Composer
On this page you can find all versions of the php package carpehora/extrapropertiesbehavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download carpehora/extrapropertiesbehavior
More information about carpehora/extrapropertiesbehavior
Files in carpehora/extrapropertiesbehavior
Package extrapropertiesbehavior
Short Description ExtraPropertiesBehavior is a propel behavior to store key/value extra fields in a propel model.
License MIT
Homepage http://carpe-hora.github.com/ExtraPropertiesBehavior/
Informations about the package extrapropertiesbehavior
ExtraPropertiesBehavior
The ExtraPropertiesBehavior provides a key/value extension for an object.
Basic example
Given a product model, ExtraPropertiesBehavior will add a key/value extension interface.
Installation
First clone the behavior in your vendor directory:
Or using Composer:
Then register behavior in either your or configuration file:
Usage
Just add the behavior to your table definition:
At this point the behavior will create an extra table to store properties and will add the following set of methods in the active record object:
Common methods
hasProperty('property_name')
countPropertiesByName('property_name')
initializeProperties()
deletePropertiesByName('property_name')
Single instance properties
setProperty('property_name', 'value')
getProperty('property_name', 'default value')
multiple instance properties
addProperty('property_name', 'value')
getPropertiesByName('property_name')
This is nice, but usualy what a developer wants is direct access through getters and setters. To do so, declare an extra properties list using the following methods:
registerProperty('property_name, 'default value')
registerMultipleProperty('property_name')
property extraction methods
getProperties()
returns an array of properties
Configuration
First declare the behavior in your :
To enable humanized getters, declare an initializeProperties()
method in your model like this:
Then you can use getters and setters directly with your model object:
Use with single inheritance
It sometimes is useful to be able to extend the model depending on the inheritance classkey. ExtraPropertiesBehavior can do that for you.
Imagine a CMS with several content types:
Given the default content structure, just define your content options by defining your possible key/values in the
initializeProperties()
method:
and
Then, just use extra properties as if it where built in fields:
Todo
- implement default properties (generate methods and register in initialize)
- parameter to chose setters and getters name.
- add a callback to convert property value
- add namespace