PHP code example of sirix / cycle-orm-factory

1. Go to this page and download the library: Download sirix/cycle-orm-factory 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/ */

    

sirix / cycle-orm-factory example snippets




declare(strict_types=1);

use Cycle\Database\Config;
use Psr\Cache\CacheItemPoolInterface;
use Sirix\Cycle\Enum\SchemaProperty;


return [
    'cycle' => [
        'db-config' => [
            'default' => 'default',
            'databases' => [
                'default' => [
                    'connection' => 'mysql',
                ]
            ],
            'connections' => [
                'mysql' => new Config\MySQLDriverConfig(
                    connection: new Config\MySQL\TcpConnectionConfig(
                        database: 'cycle-orm',
                        host: '127.0.0.1',
                        port: 3306,
                        user: 'cycle',
                        password: 'password'
                    ),
                    reconnect: true,
                    timezone: 'UTC',
                    queryCache: true,
                ),
            ]
        ],
        'migrator' => [
            'directory' => 'db/migrations',
            'table' => 'migrations',
            'seed-directory' => 'db/seeds',
        ],
        'entities' => [
            'src/App/src/Entity', // The 'entities' configuration must always be present and point to an existing directory, even when working with manual entities.
        ],
        'schema' => [
            'property' => SchemaProperty::GenerateMigrations,
            'cache' => [
                'enabled' => true,
                'key' => 'cycle_cached_schema', // optional parameter
                'service' => CacheItemPoolInterface::class, // optional parameter if psr container have 'cache' CacheItemPoolInterface service
            ]
        ],
    ],
];

'migrator' => [
    'directory'      => 'db/migrations',
    'table'          => 'migrations',
    'seed-directory' => 'db/seeds',
],

'entities' => [
    'src/App/src/Entity',
],



/**
 * Example of a Cycle ORM schema configuration.
 * Use this template to define your entities, relationships, and database mappings.
 * Note: This configuration must be placed within the 'schema' => ['manual_mapping_schema_definitions'] array.
 */

use Cycle\ORM\Relation;
use Cycle\ORM\SchemaInterface;

return [
    'cycle' => [
        'schema' => [
            'manual_mapping_schema_definitions' => [
                'example_entity' => [
                    // Entity class for mapping
                    SchemaInterface::ENTITY => YourEntity::class,
    
                    // Database and table name for the entity
                    SchemaInterface::DATABASE => 'your-database',
                    SchemaInterface::TABLE => 'your_table_name',
    
                    // Primary key column
                    SchemaInterface::PRIMARY_KEY => 'id',
    
                    // Column mappings: database columns to entity properties
                    SchemaInterface::COLUMNS => [
                        'id' => 'id',
                        'name' => 'name',
                        'createdAt' => 'created_at',
                        'updatedAt' => 'updated_at',
                    ],
    
                    // Typecasting for fields
                    SchemaInterface::TYPECAST => [
                        'id' => 'int',
                        'createdAt' => 'datetime',
                        'updatedAt' => 'datetime',
                    ],
    
                    // Optional: Custom typecast handlers
                    SchemaInterface::TYPECAST_HANDLER => YourTypecastHandler::class,
    
                    // Relationships definition
                    SchemaInterface::RELATIONS => [
                        'relatedEntities' => [
                            Relation::TYPE => Relation::HAS_MANY, // Relation type
                            Relation::TARGET => RelatedEntity::class, // Target entity class
                            Relation::SCHEMA => [
                                Relation::CASCADE => true, // Cascade updates/deletes
                                Relation::INNER_KEY => 'id', // Local key in this entity
                                Relation::OUTER_KEY => 'related_entity_id', // Foreign key in the related entity
                                Relation::WHERE => [
                                    'status' => 'active', // Optional filter for the relation
                                ],
                            ],
                        ],
                        'anotherEntity' => [
                            Relation::TYPE => Relation::BELONGS_TO,
                            Relation::TARGET => AnotherEntity::class,
                            Relation::SCHEMA => [
                                Relation::CASCADE => true,
                                Relation::INNER_KEY => 'another_entity_id',
                                Relation::OUTER_KEY => 'id',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

    'schema'   => [
        'property' => SchemaProperty::GenerateMigrations,
        'cache'    => [
            'enabled' => true,
            'key' => 'cycle_orm_cached_schema',
            'service' => CacheItemPoolInterface::class, 
        ],
    ],

$container->get('orm'); // Cycle\ORM\ORM
$container->get('dbal'); // Cycle\Database\DatabaseInterface
$container->get('migrator'); // Cycle\Migrations\Migrator



declare(strict_types=1);

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

return [
    'dependencies' => [
        'factories' => [
            'Cache\Symfony\Filesystem' => function () {
                // Create a Symfony Cache File Adapter instance
                return new FilesystemAdapter(
                    'cycle', // Namespace prefix for cache keys
                    0, // Default TTL of items in seconds (0 means infinite)
                    'data/cycle/cache' // Absolute or relative directory for cache files storage
                );
            },
        ],
    ],
];

    'cache' => [
        'enabled' => true,
        'key' => 'cycle_orm_cached_schema',
        'service' => 'Cache\Symfony\Filesystem',
    ],
bash
php vendor/bin/laminas cycle:migrator:run
bash
php vendor/bin/laminas cycle:migrator:rollback
bash
php vendor/bin/laminas cycle:migrator:create PascalCaseMigrationName
bash
php vendor/bin/laminas cycle:cache:clear
bash
php vendor/bin/laminas cycle:seed:create SeedName
bash
php vendor/bin/laminas cycle:seed:run SeedName