Skip to content

roxblnfk/cycle-active-record

Repository files navigation

Cycle Active Record

PHP Latest Version on Packagist GitHub Tests Action Status Total Downloads dependency status

ActiveRecord pattern based on Cycle ORM. AR entities work fine with mappers, repositories, behaviors and other Cycle features.

The package just adds to entity such proxy methods like save and delete using a class inheritance.

Requirements

Make sure that your server is configured with following PHP version and extensions:

Installation

You can install the package via composer:

composer require roxblnfk/cycle-active-record

After package install you need to register bootloader from the package.

Note If you are installing the package on the Yii 3 or Spiral Framework with the spiral-packages/discoverer package, then you don't need to register bootloader by yourself. It will be registered automatically.

Spiral Framework without discoverer

Update Bootloader list

protected const LOAD = [
    // ...
    \Cycle\ActiveRecord\Boot\CycleActiveRecordBootloader::class,
];

Custom application

After Container initialization just register it in AR static class:

\Cycle\ActiveRecord\StaticOrigin::setContainer($container);

Example

Entity:

use Cycle\ActiveRecord\ActiveRecord;
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity(table: 'user')]
class User extends ActiveRecord
{
    #[Column(type: 'primary', typecast: 'int')]
    public int $id;

    public function __construct(
        #[Column(type: 'string')]
        public string $name
    ) {}
}

Usage:

$user1 = new User('Lia');
$user2 = new User('Zaza');

// Persisting
$user1->prepare();
$user2->save(); // Save current and prepared entities

// Find and delete
User::findByPK(10)?->delete();

// Delete multiple
$user1->prepareDeletion();
$user2->delete();

// Use SelectQuery
User::find()->where('id', '>', '10')->fetchData();

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.