An open API service indexing awesome lists of open source software.

https://github.com/codemix/yiiabstractarraymodel

Work with PHP files in Active Record way
https://github.com/codemix/yiiabstractarraymodel

Last synced: 3 months ago
JSON representation

Work with PHP files in Active Record way

Awesome Lists containing this project

README

          

YiiAbstractArrayModel
=====================

Work with PHP files in an Yii Active Record way.

Whenever there is a bunch of PHP array files that need to be maintained through a web interface, one could do CRUD operations by extending the AbstractArrayModel class.

##Usage

Supposing we need to manage the following config files:

/path/to/config/


config1.php
config2.php
config3.php
old-config1.php
...

config1.php


return array(
'name' => 'site',
'theme' => 'bootstrap',
'components' => array(
'bill' => array(
'siteId' => 11,
'packages' => array(
8001,
10442
),
),
),
'params' => array(
'mainCssUrl' => '/css/app/site.css',
'prefix' => 'M',
)
);

We define our model by extending the AbstractArrayModel class and overriding the following methods:


class Config extends AbstractArrayModel
{

/**
* Base path definition
* @return string base path
*/
public function getBasePath(){
return '/path/to/config/';
}

/**
* File pattern matching
* @return string pattern
*/
public function getPattern(){
return '*.php';
}

/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('name, theme, components, params', 'safe'),
);
}

/**
* Returns the list of all attribute names of the model.
* @return array list of attribute names.
*/
public function attributeNames()
{
return array('name', 'theme', 'components', 'params');
}
}

Following methods are now available:


$model = Config::model()->findByPk('config1');
$model->theme = 'foundation';
$model->components['bill']['siteId'] = 99;
$model->save(); //save the file

$model->delete(); //remove the file

$models = Config::model()->findAll(); // returns all files as models

//We can also pattern match the name. Internally uses the PHP [glob] (http://php.net/manual/en/function.glob.php) method
$model = Config::model()->findA('old-*');
$models = Config::model()->findAll('config*');