PHP code example of cycle / active-record

1. Go to this page and download the library: Download cycle/active-record library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cycle / active-record example snippets




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,
            
            // ...
        ];
}

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

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;
    }
}

$user = new User(name: 'John');
$user->save();
bash
make lint-php