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.
Make sure that your server is configured with following PHP version and extensions:
- PHP 8.0+
- One of the Cycle ORM adapters:
spiral/cycle-bridge
package for the Spiral Frameworkyiisoft/yii-cycle
^2.0 package for the Yii 3
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.
Update Bootloader list
protected const LOAD = [
// ...
\Cycle\ActiveRecord\Boot\CycleActiveRecordBootloader::class,
];
After Container initialization just register it in AR static class:
\Cycle\ActiveRecord\StaticOrigin::setContainer($container);
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();
composer test
The MIT License (MIT). Please see License File for more information.