PHP code example of greg0 / lazer-database

1. Go to this page and download the library: Download greg0/lazer-database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

greg0 / lazer-database example snippets


define('LAZER_DATA_PATH', realpath(__DIR__).'/data/'); //Path to folder with tables

use Lazer\Classes\Database as Lazer; // example

Lazer::create('table_name', array(
    'id' => 'integer',
    'nickname' => 'string',
    {field_name} => {field_type}
));

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; // or $row->getField('id')

$row = Lazer::table('table_name')->where('name', '=', 'John')->find();

echo $row->id;

$row = Lazer::table('table_name');

$row->nickname = 'new_user'; // or $row->setField('nickname', 'new_user')
$row->save();

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->nickname = 'edited_user'; // or $row->setField('nickname', 'edited_user')
$row->save();

$row = Lazer::table('table_name')->find(1); //Edit row with ID 1

$row->set(array(
    'nickname' => 'user',
    'email' => '[email protected]'
));
$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();

use Lazer\Classes\Relation; // example

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();

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