roxblnfk / cycle-active-record
Cycle Active Record
Requires
- php: >=8.0
- cycle/orm: ^2.0
Requires (Dev)
- cycle/entity-behavior: ^1.0
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- spiral/boot: ^2.10
- spiral/cycle-bridge: ^1.1
- spiral/logger: ^1.10
- spiral/testing: ^1.0
- vimeo/psalm: ^4.9
This package is auto-updated.
Last update: 2024-11-09 08:05:14 UTC
README
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:
- 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
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.