Skip to content

Provides a simple way to work with your database using Active Record pattern and Cycle ORM.

License

Notifications You must be signed in to change notification settings

cycle/active-record

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Total Downloads Latest Stable Version Commits since latest release PHP Version Require Codecov Coverage Type Coverage Mutation testing badge PHP Stan Level 5 of 9 Discord Follow on Twitter (X)

Active Record Implementation for Cycle ORM

This library extends Cycle ORM by integrating the Active Record pattern, providing developers with an intuitive, object-centric way to interact with databases.

Unlike Cycle ORM's default Data Mapper pattern, which separates the in-memory object representations from database operations, Active Record combines data access and business logic in a single entity.

This allows for more straightforward and rapid development cycles, particularly for simpler CRUD operations, by enabling direct data manipulation through the object's properties and methods.


🚩 Prerequisites

Before you begin, ensure your development environment meets the following requirements:


💿 Installation

The preferred way to install this package is through Composer.

composer require cycle/active-record

After package install you need to, optionally, register bootloader / service-provider in your application.

→ Spiral Framework

Note

If you are installing the package on the 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 in your application configuration:

<?php

declare(strict_types=1);

namespace App\Application;

use Spiral\Cycle\Bootloader as CycleBridge;
use Cycle\ActiveRecord\Bridge\Spiral\Bootloader\ActiveRecordBootloader;

class Kernel extends \Spiral\Framework\Kernel
{
    public function defineBootloaders(): array
    {
        return [
            // ...
        
            // ORM
            CycleBridge\SchemaBootloader::class,
            CycleBridge\CycleOrmBootloader::class,
            CycleBridge\AnnotatedBootloader::class,
            
            // ActiveRecord
            ActiveRecordBootloader::class,
            
            // ...
        ];
}

For more information about bootloaders, refer to the Spiral Framework documentation.

→ Laravel

Note

If you are using Laravel, then you don't need to register service-provider by yourself. It will be registered automatically.

→ Yii 3

For configuration instructions refer to yii-cycle installation guide.

→ Other Frameworks

This package uses PSR-11 compatible container to resolve dependencies. After container initialization you need to pass container instance to the static facade:

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

📖 Usage

Note

For detailed usage instructions, refer to the documentation.

→ Basic Example

Define Entity with ActiveRecord

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

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

    #[Column(type: 'string')]    
    private string $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }
    
    public function id(): int
    {
        return $this->id;
    }
    
    public function name()
    {
        return $this->name;
    }
}

Create a new record

$user = new User(name: 'John');
$user->save();

🧪 Running Tests

→ PHPUnit tests

To run tests, run the following command:

make test

→ Mutation tests

To run mutation tests, using infection/infection:

make infect

→ Static Analysis

Code quality using PHPStan:

make lint-stan

and using Psalm:

make lint-psalm

→ Coding Standards Fixing

Fix code using The PHP Coding Standards Fixer (PHP CS Fixer) to follow our standards:

make lint-php

→ Lint Yaml files

Lint all yaml files in project:

make lint-yaml

→ Lint Markdown files

Lint all yaml files in project:

make lint-md

→ Lint GitHub Actions

Lint all yaml files in project:

make lint-actions

🔒 Security Policy

This project has a security policy.


🙌 Want to Contribute?

Thank you for considering contributing to the cycle community! We are open to all kinds of contributions. If you want to:

You are more than welcome. Before contributing, kindly check our contribution guidelines.

Conventional Commits


🫡 Contributors

Contributors Badge

🌐 Social Links

  • Twitter: Follow our organization @SpiralPHP.
  • Discord: Join our community on Discord.

⚖️ License

Licence


About

Provides a simple way to work with your database using Active Record pattern and Cycle ORM.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •