1. Go to this page and download the library: Download icanboogie/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/ */
icanboogie / activerecord example snippets
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Validate\ValidateActiveRecord;
use ICanBoogie\ActiveRecord\ActiveRecordCache\RuntimeActiveRecordCache;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\Prototype;
Prototype::configure([
ActiveRecord::class => [
'validate' => function(ActiveRecord $record) {
static $validate;
$validate ??= new ValidateActiveRecord;
return $validate($record);
}
],
Model::class => [
'lazy_get_activerecord_cache' => fn(Model $model) =>
new RuntimeActiveRecordCache($model),
]
]);
use ICanBoogie\ActiveRecord\Connection;
# a connection to a MySQL database
$connection = new Connection('mysql:dbname=example', 'username', 'password');
# a connection to a SQLite temporary database stored in memory
$connection = new Connection('sqlite::memory:');
/* @var $connection \ICanBoogie\ActiveRecord\Connection */
$connection('ALTER TABLE nodes CHARACTER SET "{charset}" COLLATE "{collate}"');
namespace App\Modules\Nodes;
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\Schema\Character;
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Integer;
use ICanBoogie\ActiveRecord\Schema\Serial;
/**
* @extends Model<int, Node>
*/
#[Model\Record(Node:::class)]
class NodeModel extends Model
{
}
/**
* @extends ActiveRecord<int>
*/
class Node extends ActiveRecord
{
#[Id, Serial]
public int $id;
#[Character(80)]
public string $title;
#[Integer]
public int $number;
// …
}
$config = (new ActiveRecord\ConfigBuilder())
->use_attributes()
->add_connection(/*...*/)
->add_record(NodeModel::class)
->build();
/* @var $connections ConnectionCollection */
$models = new ModelCollection($connections, $config->models);
$models->install();
$node_model = $models->model_for_record(Node::class);
$node = new Node($node_model);
// ^^^^^^^^^^^
// because we don't use a model provider yet, we need to specify the model to the active record
$node->title = "My first node";
$node->number = 123;
$id = $node->save();
# or
$id = $node->id;
echo "Saved node, got id: $id\n";
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;
use ICanBoogie\ActiveRecord\Schema\BelongsTo;
use ICanBoogie\ActiveRecord\Schema\Character;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\SchemaBuilder;
class Article extends ActiveRecord
{
#[Id, Serial]
public int $id;
#[BelongsTo(User::class)]
public int $uid;
}
class User extends ActiveRecord
{
#[Id, Serial]
public int $uid;
#[Character]
public string $name;
}
// …
/* @var $news Model */
$record = $news->query()->one;
echo "{$record->title} belongs to {$record->user->name}.";
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;
use ICanBoogie\ActiveRecord\Schema\BelongsTo;
use ICanBoogie\ActiveRecord\Schema\Character;
use ICanBoogie\ActiveRecord\Schema\HasMany;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\SchemaBuilder;
class Article extends ActiveRecord
{
#[Id, Serial]
public int $id;
#[BelongsTo(User::class)]
public int $uid;
}
#[HasMany(Article::class)]
class User extends ActiveRecord
{
#[Id, Serial]
public int $uid;
#[Character]
public string $name;
}
// …
/* @var $user User */
foreach ($user->articles as $article) {
echo "User {$user->name} has article {$article->id}.";
}
namespace App;
use ICanBoogie\ActiveRecord;
class Node extends ActiveRecord
{
// …
protected function get_next()
{
return $this->model->own->visible->where('date > ?', $this->date)->order('date')->one;
}
protected function get_previous()
{
return $this->model->own->visible->where('date < ?', $this->date)->order('date DESC')->one;
}
// …
}
$record = Article::from([
'title' => "An example",
'body' => "My first article",
'language' => "en",
'is_online' => true
]);
use ICanBoogie\ActiveRecord;
// …
class User extends ActiveRecord
{
use ActiveRecord\Property\CreatedAtProperty;
use ActiveRecord\Property\UpdatedAtProperty;
#[Id, Serial]
public int $id;
#[Character]
public string $username;
#[Character(unique: true)]
public string $email;
// …
/**
* @inheritdoc
*/
public function create_validation_rules()
{
return [
'username' => '
namespace App;
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Property\CreatedAtProperty;
use ICanBoogie\ActiveRecord\Property\UpdatedAtProperty;
class Node extends ActiveRecord
{
public $title;
use CreatedAtProperty;
use UpdatedAtProperty;
}
$node = new Node;
echo get_class($node->created_at); // ICanBoogie\Datetime
echo $node->created_at->is_empty; // true
$node->created_at = 'now';
echo $node->created_at; // 2014-02-21T15:00:00+0100
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\ConnectionCollection;
$connections = new ConnectionCollection([
new ConnectionDefinition(id: 'read', dsn: 'sqlite::memory:'),
new ConnectionDefinition(id: 'write', dsn: 'mysql:dbname=my_database'),
]);
$connection = $connections->connection_for_id('read');
/* @var $connections \ICanBoogie\ActiveRecord\ConnectionCollection */
if (isset($connections->definitions['one']))
{
echo "The connection 'one' is defined.\n";
}
/* @var $connections \ICanBoogie\ActiveRecord\ConnectionCollection */
foreach ($connections->established as $id => $connection)
{
echo "The connection '$id' is established.\n";
}
/* @var $connections \ICanBoogie\ActiveRecord\ConnectionCollection */
foreach ($connections as $id => $connection)
{
echo "The connection '$id' is established.\n";
}
/* @var $models \ICanBoogie\ActiveRecord\ModelCollection */
foreach ($models->instances as $class => $model)
{
echo "The model '$class' has been instantiated.\n";
}
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\StaticModelProvider;
/* @var $provider ActiveRecord\ModelProvider */
StaticModelProvider::set(fn() => $provider);
$nodes = StaticModelProvider::model_for_record(Node::class);
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\Prototype;
Prototype::from('ICanBoogie\ActiveRecord\Model')['lazy_get_activerecord_cache'] = function(Model $model) {
return new MyActiveRecordCache($model);
};