PHP Library to use JSON files like a database.
Functionality inspired by ORM's
- PHP 5.4+
- Composer
Easiest way to install Lazer Database
is to use Composer. Of course you can use your own autoloader but you must configure it properly by yourself. You can find my Package on Packagist.org.
Add the following to your composer.json
and run composer update
.
"require": {
"greg0/lazer-database": "1.0.7"
}
table_name.data.json
- table file with data
table_name.config.json
- table file with configuration
First of all you should define constant LAZER_DATA_PATH
containing absolute path to folder with JSON files:
define('LAZER_DATA_PATH', realpath(dirname(__FILE__)).'/data/'); //Path to folder with tables
Then set up namespace:
use Lazer\Classes\Database as Lazer; // example
limit()
- returns results between a certain number range. Should be used right before ending methodfind_all()
.orderBy()
- sort rows by key in order, can order by more than one field (just chain it).groupBy()
- group rows by field.where()
- filter records. Alias:and_where()
.orWhere()
- other type of filtering results.with()
- join other tables by defined relations
addFields()
- append new fields into existing tabledeleteFields()
- removing fields from existing tablesave()
- insert or Update data.delete()
- deleting data.relations()
- returns array with table relationsconfig()
- returns object with configuration.fields()
- returns array with fields name.schema()
- returns assoc array with fields name and fields typefield => type
.lastId()
- returns last ID from table.count()
- returns the number of rows.find()
- returns one row with specified ID.findAll()
- returns rows.asArray()
- returns data as indexed or assoc array:['field_name' => 'field_name']
. Should be used after ending methodfind_all()
.
Lazer::create('table_name', array(
'id' => 'integer',
'nickname' => 'string',
{field_name} => {field_type}
));
More informations about field types and usage in PHPDoc
Lazer::remove('table_name');
try{
\Lazer\Classes\Helpers\Validate::table('table_name')->exists();
} catch(\Lazer\Classes\LazerException $e){
//Database doesn't exist
}
$table = Lazer::table('table_name')->findAll();
foreach($table as $row)
{
print_r($row);
}
$row = Lazer::table('table_name')->find(1);
echo $row->id;
Type ID of row in find()
method.
You also can do something like that to get first matching record:
$row = Lazer::table('table_name')->where('name', '=', 'John')->find();
echo $row->id;
$row = Lazer::table('table_name');
$row->nickname = 'new_user';
$row->save();
Do not set the ID.
It's very smilar to Inserting
.
$row = Lazer::table('table_name')->find(1); //Edit row with ID 1
$row->nickname = 'edited_user';
$row->save();
Lazer::table('table_name')->find(1)->delete(); //Will remove row with ID 1
// OR
Lazer::table('table_name')->where('name', '=', 'John')->find()->delete(); //Will remove John from DB
Lazer::table('table_name')->where('nickname', '=', 'edited_user')->delete();
Lazer::table('table_name')->delete();
To work with relations use class Relation
use Lazer\Classes\Relation; // example
belongsTo
- relation many to onehasMany
- relation one to manyhasAndBelongsToMany
- relation many to many
belongsTo()
- set relation belongsTohasMany()
- set relation hasManyhasAndBelongsToMany()
- set relation hasAndBelongsToManylocalKey()
- set relation local keyforeignKey()
- set relation foreign keywith()
- allow to work on existing relation
setRelation()
- creating specified relationremoveRelation()
- creating specified relationgetRelation()
- return informations about relation
Relation::table('table1')->belongsTo('table2')->localKey('table2_id')->foreignKey('id')->setRelation();
Relation::table('table2')->hasMany('table1')->localKey('id')->foreignKey('table2_id')->setRelation();
Relation::table('table2')->hasAndBelongsToMany('table1')->localKey('id')->foreignKey('id')->setRelation(); // Junction table will be crete automaticly
Relation::table('table1')->with('table2')->removeRelation();
You can do it by two ways. Use Standard Database class or Relation but results will be different.
Relation::table('table1')->with('table2')->getRelation(); // relation with specified table
Lazer::table('table1')->relations(); // all relations
Lazer::table('table1')->relations('table2'); // relation with specified table
For some examples please check examples.md
and tutorial.md
file.
More informations you can find in PHPDoc, I think it's documented very well.
Homepage: http://greg0.ovh.org
E-mail: [email protected]
If you like and using/want to use my repo or you have any suggestions I will be greatful for sending me few words on e-mail.