1. Go to this page and download the library: Download voku/simple-active-record 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/ */
voku / simple-active-record example snippets
use voku\db\DB;
ance('yourDbHost', 'yourDbUser', 'yourDbPassword', 'yourDbName');
// example
// $db = DB::getInstance('localhost', 'root', '', 'test');
$user = new User();
$user->name = 'demo';
$user->password = password_hash('demo', PASSWORD_BCRYPT, ["cost" => 15]);
$user_id = $user->insert();
var_dump($user_id); // the new id
var_dump($user->id); // also the new id
var_dump($user->getPrimaryKey()); // also the new id
$user = new User();
$user->notnull('id')->orderBy('id desc')->fetch();
// OR //
$user->fetch(1);
// OR //
$user->fetchById(1); // thows "FetchingException" if the ID did not exists
// OR //
$user->fetchByIdIfExists(1); // return NULL if the ID did not exists
// OR //
$user->fetchByHashId('fsfsdwldasdar'); // thows "FetchingException" if the ID did not exists
// OR //
$user->fetchByHashIdIfExists('fsfsdwldasdar'); // return NULL if the ID did not exists
var_dump($user->id); // (int) 1
var_dump($user->getPrimaryKey()); // (int) 1
$user = new User();
$users = $user->fetchAll();
// OR //
$users = $user->fetchByIds([1]);
// OR //
$users = $user->fetchByIdsPrimaryKeyAsArrayIndex([1]);
var_dump($users[0]->id) // (int) 1
var_dump($users[0]->getPrimaryKey()); // (int) 1
$user = new User();
$user->select('id', 'name')->fetch();
$user = new User();
$user->select('id', 'name')->from('user')->fetch();
$user = new User();
$user->join('contact', 'contact.user_id = user.id')->fetch();
$user = new User();
$user->where('id=1 AND name="demo"')->fetch();
$user = new User();
$user->select('count(1) as count')->groupBy('name')->fetchAll();
$user = new User();
$user->orderBy('name DESC')->fetch();
$user = new User();
$user->orderBy('name DESC')->limit(0, 1)->fetch();
$user = new User();
$user->eq('id', 1)->fetch();
$user = new User();
$user->ne('id', 1)->fetch();
$user = new User();
$user->gt('id', 1)->fetch();
$user = new User();
$user->lt('id', 1)->fetch();
$user = new User();
$user->ge('id', 1)->fetch();
$user = new User();
$user->le('id', 1)->fetch();
$user = new User();
$user->like('name', 'de')->fetch();
$user = new User();
$user->in('id', [1, 2])->fetch();
$user = new User();
$user->notin('id', [1, 3])->fetch();
$user = new User();
$user->isnull('id')->fetch();
$user = new User();
$user->isNotNull('id')->fetch();
use voku\db\DB;
use voku\db\ActiveRecord;
, 'YOUR_MYSQL_USER', 'YOUR_MYSQL_PW', 'YOUR_DATABASE');
ActiveRecord::setDb($db);
namespace demo;
use voku\db\ActiveRecord;
/**
* @property int $id
* @property string $name
* @property string $password
* @property Contact[] $contacts
* @property Contact $contacts_with_backref
* @property Contact $contact
*/
class User extends ActiveRecord {
public $table = 'user';
public $primaryKey = 'id';
protected function init()
{
$this->addRelation(
'contacts',
self::HAS_MANY,
FoobarContact::class,
'user_id'
);
$this->addRelation(
'contacts_with_backref',
self::HAS_MANY,
FoobarContact::class,
'user_id',
null,
'user'
);
$this->addRelation(
'contact',
self::HAS_ONE,
FoobarContact::class,
'user_id',
[
self::SQL_WHERE => '1 = 1',
self::SQL_ORDER => 'id desc',
]
);
}
}
/**
* @property int $id
* @property int $user_id
* @property string $email
* @property string $address
* @property User $user_with_backref
* @property User $user
*/
class Contact extends ActiveRecord {
public $table = 'contact';
public $primaryKey = 'id';
protected function init()
{
$this->addRelation(
'user_with_backref',
self::BELONGS_TO,
FoobarUser::class,
'user_id',
null,
'contact'
);
$this->addRelation(
'user',
self::BELONGS_TO,
FoobarUser::class,
'user_id'
);
}
}
use demo\User;
$user = new User();
$user->name = 'demo';
$user->password = password_hash('demo', PASSWORD_BCRYPT, ["cost" => 15]);
$user_id = $user->insert();
var_dump($user_id); // the new id
var_dump($user->id); // also the new id
var_dump($user->getPrimaryKey()); // also the new id
use demo\Contact;
$contact = new Contact();
$contact->address = 'test';
$contact->email = '[email protected]';
$contact->user_id = $user->id;
var_dump($contact->insert()); // the new id
var_dump($contact->id); // also the new id
var_dump($contact->getPrimaryKey()); // also the new id
use demo\User;
use demo\Contact;
$user = new User();
// fetch one user
var_dump($user->notnull('id')->orderBy('id desc')->fetch());
echo "\nContact of User # {$user->id}\n";
// get contacts by using relation:
// 'contacts' => [self::HAS_MANY, 'demo\Contact', 'user_id'],
var_dump($user->contacts);
$contact = new Contact();
// fetch one contact
var_dump($contact->fetch());
// get user by using relation:
// 'user' => [self::BELONGS_TO, 'demo\User', 'user_id'],
var_dump($contact->user);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.