PHP code example of zein / database

1. Go to this page and download the library: Download zein/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/ */

    

zein / database example snippets




namespace App\Models;

use PDO;
use Zein\Database\Dages\ModelContract;
use Zein\Database\Connection\Driver\Mysql\Dsn;

class ParentModel extends ModelContract
{
    use Dsn;

    protected function createConnectionInit()
    {
        $this->ConnectionProfile = [
            'dsn' => Dsn::init(['host' => 'DatabaseHost', 'port' => 'DatabasePort', 'dbname' => 'DatabaseName']),
            'username' => 'DatabaseUsername',
            'password' => 'Database Password',
            'options' => [
                [PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)]
            ]
        ];
    }
}


namespace App\Models;

// Mendoan.php and ParentModel.php in same directory
class Mendoan extends ParentModel
{
    /**
     * custom table name
     * if your table is not same with this object name
     *
     * Default Table : Mendoan
     **/
     // protected $Table = ''; // fill it your table name
     
    /**
     * custom PrimaryKey column
     * 
     * Default column : id
     **/
     // protected $PrimaryKey = '';
}



use App\Models\Mendoan;

// Short Hand way - retrive all column based primarykey
$Mendoan = Mendoan::find(1);

// Spesific column
$Mendoan = Mendoan::select('ingredients','cooked_at','expired_at')
                      ->get();

// With criteria
$Mendoan = Mendoan::select('ingredients','cooked_at','expired_at')
                      ->where('beangrade', 'good')
                      ->get();
                      
// With join
$Mendoan = Mendoan::select('ingredients','cooked_at','expired_at')
                      ->from('mendoan AS m')
                      ->innerJoin('bean AS b', ['b.grade', '=', 'm.beangrade'])
                      ->where('m.beangrade', 'good')
                      ->get();



use App\Models\Mendoan;

// Short Hand way - Set column as data and save it
$Mendoan = new Mendoan; // create instance first

// set the column as $Mendoan property
$Mendoan->ingredients = 'Bean, Yeast';

// call save method to insert data
$Mendoan->save();

// or use create method
$Mendoan = Mendoan::create(['ingredients' => 'Bean, Yeast']);