You can avoid the usage of the cycle/annotation
extension or any other schema related extension and write the ORM
mapping schema manually.
You can define a custom schema by passing a Schema
object into your ORM instance:
$orm = $orm->with(schema: new \Cycle\ORM\Schema([
// ... schema ...
]));
To add a simple POPO entity mapping:
use Cycle\ORM\Schema;
use Cycle\ORM\Mapper\Mapper;
$orm = $orm->with(schema: new Schema([
'user' => [
Schema::ENTITY => User::class,
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => [
// property => column
'id' => 'id',
'email' => 'email',
'balance' => 'balance'
],
Schema::TYPECAST => [
'id' => 'int',
'balance' => 'float'
],
Schema::RELATIONS => []
]
]));
To define the relation you must ensure that both entities are added to the schema and then populate RELATION section:
use Cycle\ORM\Schema;
use Cycle\ORM\Relation;
use Cycle\ORM\Mapper\Mapper;
$orm = $orm->with(schema: new Schema([
'user' => [
Schema::ENTITY => User::class,
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'email', 'balance'],
Schema::RELATIONS => [
'profile' => [
Relation::TYPE => Relation::HAS_ONE,
Relation::TARGET => 'profile',
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'user_id',
],
]
]
],
'profile' => [
Schema::ENTITY => Profile::class,
Schema::MAPPER => Mapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'profile',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'user_id', 'image'],
Schema::RELATIONS => []
],
]));
Extra fields are omitted.
You can see other relation examples here.