-
Hello, I am new to cycle/orm and I decided to go without annotations:
My code: $dbal = new \Cycle\Database\DatabaseManager(
new \Cycle\Database\Config\DatabaseConfig([
'databases' => [
'default' => ['connection' => 'postgres'],
],
'connections' => [
'postgres' => new \Cycle\Database\Config\PostgresDriverConfig(
connection: new \Cycle\Database\Config\Postgres\TcpConnectionConfig(
[...]
),
[...]
),
]]);
$consoleLogger = new \App\Infrastructure\ConsoleLogger();
$dbal->setLogger($consoleLogger);
$registry = new \Cycle\Schema\Registry($dbal);
$entity = new \Cycle\Schema\Definition\Entity();
$entity
->setRole('user')
->setClass(\App\Entity\ApiUser::class);
$entity->getFields()
->set('id', (new \Cycle\Schema\Definition\Field())->setType('string(36)')->setColumn('id')->setPrimary(true))
->set('token', (new \Cycle\Schema\Definition\Field())->setType('string(28)')->setColumn('token'));
// register entity
$registry->register($entity);
// associate table
$registry->linkTable($entity, 'default', 'api_user');
$schemaArray = (new \Cycle\Schema\Compiler())->compile($registry);
$orm = new \Cycle\ORM\ORM(new \Cycle\ORM\Factory($dbal), new \Cycle\ORM\Schema($schemaArray));
$dbal->database()->execute('DROP TABLE IF EXISTS api_user');
if (!$dbal->database()->table('api_user')->exists()) {
/** @var \Cycle\Database\Schema\AbstractTable $table */
$table = $dbal->database()->table('api_user')->getSchema();
$table->save();
}
$manager = new \Cycle\ORM\EntityManager($orm);
$manager->persist(new \App\Entity\ApiUser());
$result = $manager->run(); When I take a look in the log, I see the following line (aside from others):
and later on
I also tried with non-dynamic schema building, but I ended up with the same error message. Is this a bug or am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
$schemaArray = (new \Cycle\Schema\Compiler())->compile($registry); To compile Registry to Schema you should use Compile Generators. For example: use Cycle\Schema;
$schemaArray = (new Schema\Compiler())->compile(new Schema\Registry($dbal), [
new Schema\Generator\GenerateRelations(), // generate entity relations
new Schema\Generator\GenerateModifiers(), // generate changes from schema modifiers
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 Schema\Generator\RenderModifiers(), // render all schema modifiers
new Schema\Generator\SyncTables(), // sync table changes to database
new Schema\Generator\GenerateTypecast(), // typecast non string columns
]); See: https://cycle-orm.dev/docs/intro-install/2.x/en#define-entity-schema-generation $dbal->database()->execute('DROP TABLE IF EXISTS api_user');
if (!$dbal->database()->table('api_user')->exists()) {
/** @var \Cycle\Database\Schema\AbstractTable $table */
$table = $dbal->database()->table('api_user')->getSchema();
$table->save();
} DBAL (cycle/database package) doesn't use the compiled Schema. This code will always create an empty table. |
Beta Was this translation helpful? Give feedback.
To compile Registry to Schema you should use Compile Generators. For example: