1. Go to this page and download the library: Download mathsgod/light-db 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/ */
mathsgod / light-db example snippets
use Light\Db\Model;
class User extends Model
{
// Uses class name as table name by default
// Customize with: protected static $_table = 'custom_table_name';
}
class Post extends Model
{
protected static $_table = 'posts';
}
use Light\Db\Model;
class User extends Model {}
// WHERE (age >= 18 OR name = 'Peter')
$users = User::Query()->filters([
'_or' => [
['age' => ['gte' => 18]],
['name' => 'Peter']
]
])->toArray();
// WHERE status = 'active' AND (age >= 30 OR name = 'Test User 1')
$users = User::Query()
->where(['status' => 'active'])
->filters([
'_or' => [
['age' => ['gte' => 30]],
['name' => 'Test User 1']
]
])
->toArray();
// WHERE (age > 28 AND status = 'active')
// OR (name = 'Test User 3' AND status = 'inactive')
$users = User::Query()->filters([
'_or' => [
[
'_and' => [
['age' => ['gt' => 28]],
['status' => 'active']
]
],
[
'_and' => [
['name' => 'Test User 3'],
['status' => 'inactive']
]
]
]
])->toArray();
use Laminas\Db\Sql\Predicate\PredicateSet;
// WHERE (age >= 30 OR name = 'Test User 1')
$users = User::Query()
->where(['age >= ?' => 30], PredicateSet::OP_OR)
->where(['name = ?' => 'Test User 1'], PredicateSet::OP_OR)
->toArray();
// Assuming UserList model with user_id column
$user = User::Get(1);
// Get related UserList query object
$userLists = $user->UserList; // Returns Query object
// Further querying
$activeLists = $user->UserList
->filters(['status' => 'active'])
->sort('created_at:desc')
->toArray();
use Light\Db\Model;
class Schedule extends Model
{
protected static function filterDefinitions(): array
{
return [
'Letter' => function ($value) {
return "letter_id IN (SELECT letter_id FROM letter WHERE subject = " . $value . ")";
},
];
}
protected static function orderDefinitions(): array
{
return [
'recent' => fn($dir) => "created_at $dir",
];
}
}
class Letter extends Schedule
{
protected static function filterDefinitions(): array
{
return array_merge(parent::filterDefinitions(), [
'Tag' => fn($v) => "letter_id IN (SELECT letter_id FROM letter_tag WHERE tag = $v)",
]);
}
}
// Legacy: register a sort ad-hoc from anywhere (still supported)
User::RegisterOrder('popular', function($query) {
return $query->order(['score DESC', 'views DESC']);
});
// Use custom sorting
$popularUsers = User::Query()->sort('popular')->toArray();
// cursor() returns a Laravel LazyCollection — perfect for streaming large result sets
foreach (User::Query()->cursor() as $user) {
echo $user->name;
}
// execute() returns a regular Collection eagerly materialized
$users = User::Query()->execute()->toArray();
// The framework already binds values from where() conditions automatically,
// so this is normally a no-op. It exists for symmetry and advanced cases
// (e.g. when the Select contains Expression objects with named placeholders).
User::Query(['status' => 'active'])->execute([
'minAge' => 18,
])->toArray();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.