jails / li3_behaviors
Model behaviors for the Lithium PHP framework
Installs: 1 631
Dependents: 3
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 4
Open Issues: 0
Type:lithium-library
Requires
- php: >=5.4
- composer/installers: dev-master
This package is auto-updated.
Last update: 2024-11-29 04:14:20 UTC
README
Requirement
PHP 5.4
Installation
Checkout the code to either of your library directories:
cd libraries
git clone git@github.com:jails/li3_behaviors.git
Include the library in your /app/config/bootstrap/libraries.php
Libraries::add('li3_behaviors');
Presentation
Model behaviors providing a simple way to extend models. This pattern allow common logic to be encapsulated inside behaviors for keeping models lite and composed only by its own business logic.
API
Simple model creation attached to a behavior:
<?php //app/models/Posts.php namespace app\models; use li3_behaviors\data\model\Behaviors; class Posts extends \lithium\data\Model { use Behaviors; protected $_actsAs = ['Slug' => [ 'fields' => ['title' => 'title', 'name' => 'slug'] ]]; } ?>
<?php //app/extensions/data/behavior/Slug.php namespace app\extensions\data\behavior; use lithium\util\Inflector; class Slug extends \li3_behaviors\data\model\Behavior { public function __construct(array $config = []) { $defaults = [ 'fields' => ['label' => 'slug'] ]; parent::__construct($config + $defaults); } protected function _init() { parent::_init(); if ($model = $this->_model) { $behavior = $this; $model::applyFilter('save', function($self, $params, $chain) use ($behavior) { $params = $behavior->invokeMethod('_slug', array($params)); return $chain->next($self, $params, $chain); }); } } protected function _slug($params) { extract($this->_config); foreach ($fields as $from => $to) { if (isset($params['data'][$from])) { $params['data'][$to] = Inflector::slug($params['data'][$from]); } } return $params; } } ?>
Greetings
The li3 team, Nateabele's filters system and all others which make that possible (including my parents which I love).