Coder Social home page Coder Social logo

cycle / active-record Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 1.7 MB

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

Home Page: https://cycle-orm.dev

License: MIT License

PHP 82.40% Makefile 17.60%
active-record activerecord cycle cycle-orm database orm php

active-record's Introduction

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


active-record's People

Contributors

github-actions[bot] avatar lotyp avatar roxblnfk avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

active-record's Issues

[Feature]: replace ::transact() with ::group()

Is your feature request related to a problem? Please describe.

Meaning of ::transact() method may be not clear.
A programmer may think that method opens a transaction before the callable call and closes after it.
But the method just groups OPRM actions inside using EM transaction mode option. And all the custom SQL queries mill not be included in the same EM's ORM transaction.

Describe the solution you'd like

  • rename ::transact() method to ::group()
  • add a new ::transact() method that will open and close a transaction

Describe alternatives you've considered

No response

Additional context

No response

[Feature]: add Entity::make()

Is your feature request related to a problem? Please describe.

It's not recommended to create entities using constructor. Best practice is use ORM::make().
But with AR patter we should have some syntax to avoid using ORM directly

Describe the solution you'd like

Add ActiveRecord::make(array $data) method that will use ORM::make under the hood

Describe alternatives you've considered

No response

Additional context

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.