// Cycle DBAL
DatabaseManager::class => function (ContainerInterface $container) {
return new Database\DatabaseManager(
new Database\Config\DatabaseConfig(require __DIR__ . '/connection.php')
);
},
DatabaseProviderInterface::class => static function (ContainerInterface $container) {
return $container->get(DatabaseManager::class);
},
// Cycle ORM
ORMInterface::class => function (ContainerInterface $container) {
$dbal = $container->get(Spiral\Database\DatabaseManager::class);
$orm = new Cycle\ORM\ORM(new Cycle\ORM\Factory($dbal), $container->get(SchemaInterface::class));
return $orm;
},
// Schema
SchemaInterface::class => static function (ContainerInterface $container) {
return new \Cycle\ORM\Schema($container->get(Schema::class));
},
Schema::class => static function (ContainerInterface $container) {
// Class locator
$cl = (new Tokenizer\Tokenizer(new Tokenizer\Config\TokenizerConfig([
'directories' => ['../src'],
])))->classLocator();
$dbal = $container->get(Spiral\Database\DatabaseManager::class);
$schema = (new Schema\Compiler())->compile(new Schema\Registry($dbal), [
new Annotated\Embeddings($cl), // register embeddable entities
new Annotated\Entities($cl), // register annotated entities
new Schema\Generator\ResetTables(), // re-declared table schemas (remove columns)
new Annotated\MergeColumns(), // copy column declarations from all related classes (@Table annotation)
new Schema\Generator\GenerateRelations(), // generate entity relations
new Schema\Generator\ValidateEntities(), // make sure all entity schemas are correct
new Schema\Generator\RenderTables(), // declare table schemas
new Schema\Generator\RenderRelations(), // declare relation keys and indexes
new Annotated\MergeIndexes(), // copy index declarations from all related classes (@Table annotation)
new Schema\Generator\SyncTables(), // sync table changes to database
new Schema\Generator\GenerateTypecast(), // typecast non string columns
]);
return $schema;
},
<?php
namespace App\Domain\User;
use Cycle\Annotated\Annotation as Cycle;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Table;
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Embeddable;
use DateTimeImmutable;
/**
* @Entity(
* table="users",
* role="user",
* repository="App\Domain\User\UserRepository"
* )
*/
class User
{
/** @Column(type = "primary") */
protected $id;
/** @Column(type = "string(64)", name = "username") */
protected $username;
/** @Column(type = "string(64)", name = "email") */
protected $email;
/** @Column(type = "string(64)") */
protected $password;
/** @Column(type = "string(128)", nullable = true) */
protected $token;
/** @Column(type = "tinyInteger")*/
protected $status;
protected DateTimeImmutable $created_at;
protected DateTimeImmutable $updated_at;
}
<?php
namespace App\Domain\User;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Transaction;
class UserRepository extends Select\Repository
{
private ORMInterface $orm;
public function __construct(Select $select, ORMInterface $orm)
{
$this->orm = $orm;
parent::__construct($select);
}
}
<?php
namespace App\Http\Action;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Database;
use Spiral\Database\DatabaseManager;
use Cycle\ORM;
use Cycle\ORM\ORMInterface;
use App\Domain\User;
use App\Domain\User\UserRepository;
final class UserAction
{
private UserRepository $repository;
private ORMInterface $orm;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$data = $this->repository->findByPk(1);
$response->getBody()->write(
json_encode((array)$data, JSON_THROW_ON_ERROR)
);
return $response->withHeader('Content-Type', 'application/json');
return $response;
}
}