PHP code example of mattvb91 / lightmodel

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

    

mattvb91 / lightmodel example snippets


$pdo = new PDO(/* Your PDO params*/);
LightModel::init($pdo);
     

$options = [
    LightModel::LightModel::OPTIONS_TYPECAST,
];

LightModel::init($pdo, $options);

namespace Project;

use mattvb91\LightModel\LightModel;

class User extends LightModel
{

    //
}


namespace Project;

use mattvb91\LightModel\LightModel;

class User extends LightModel
{
    public $username;
    
    public function getValues()
    {
        return [
            'username' => $this->username,
        ];
    }
}


$user = new User();
$user->username = 'Name';
$user->save();

    protected $tableName = 'new_name';
    protected $key = 'new_key';

$user = User::getOneByKey(1);
//$user now loaded with values from DB

$user->exists(); //Returns true or false

$user->refresh();

$user->delete();


public function department() 
{
    return $this->belongsTo(Department::class, 'department_id');
    //returns a loaded Department::class instance
}


$users = User::getItems();

$filter = [
    'username' => 'joe'
];

$allJoeUsers = User::getItems($filter);


$filter = [
    'username' => ['>=', 'joe']
];

$allJoeUsers = User::getItems($filter);


$filter = [
    LightModel::FILTER_ORDER => 'id DESC',
    LightModel::FILTER_LIMIT => 100;
];

$filteredUsers = User::getItems($filter);


$filter = [
    LightModel::FILTER_ORDER => 'id DESC',
];

$userKeys = User::getKeys($filter);
//We now have an array consisting of all the primary keys that
//match our criteria

foreach($userKeys as $primaryKey) 
{
    //Load the full individual record for further processing
    $user = User::getOneByKey($primaryKey);
}
Class::getOneByKey($key)