1. Go to this page and download the library: Download bephp/activerecord 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/ */
bephp / activerecord example snippets
class User extends ActiveRecord{
public $table = 'user';
public $primaryKey = 'id';
public $relations = array(
'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),
'contact' => array(self::HAS_ONE, 'Contact', 'user_id'),
);
}
class Contact extends ActiveRecord{
public $table = 'contact';
public $primaryKey = 'id';
public $relations = array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'user_with_backref' => array(self::BELONGS_TO, 'User', 'user_id', array(), 'contact'),
// using 5th param to define backref
);
}
ActiveRecord::setDb(new PDO('sqlite:test.db'));
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY,
name TEXT,
password TEXT
);");
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS contact (
id INTEGER PRIMARY KEY,
user_id INTEGER,
email TEXT,
address TEXT
);");
$user = new User();
// find one user
var_dump($user->notnull('id')->orderby('id desc')->find());
echo "\nContact of User # {$user->id}\n";
// get contacts by using relation:
// 'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),
var_dump($user->contacts);
$contact = new Contact();
// find one contact
var_dump($contact->find());
// get user by using relation:
// 'user' => array(self::BELONGS_TO, '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.