PHP code example of apitin / database

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

    

apitin / database example snippets


$users = new Apitin\Database\Select('users');
$users->where('is_active = ?', 1);
echo count($users->all()) . PHP_EOL;

#[Table("users")]
#[Column("name")]
#[Column("logged_at", type: Column::TYPE_DATETIME)]
class User extends Apitin\Database\Record
{

}

Table(string $tableName, string $primaryKey = 'id', bool $timeStamps = false, bool $softDelete = false)

Column(string $name, string $type = Column::TYPE_STRING, bool $

const   TYPE_STRING     = 'string';
const   TYPE_INTEGER    = 'int';
const   TYPE_DECIMAL    = 'decimal';
// Converted to/from boolean <-> int
const   TYPE_BOOLEAN    = 'bool';
// Converted to/from DateTimeImmutable
const   TYPE_DATETIME   = 'datetime';
// Converted to/from DateTimeImmutable
const   TYPE_DATE       = 'date';

$user = User::create([
    'name'  => 'Test User',
]);
$user->save();

$user = User::load(5);

$user = User::load(5);
$user->name = 'Updated Test User';
$user->save();

$user = User::load(5);
$user->destroy();

$user = User::select()->where('name = ?', 'Test User')->first();

$select = User::select()->where('id > 6');
$users = $select->all();
echo count($users) . PHP_EOL;