@@ -23,7 +23,11 @@ composer require cycle/orm
2323
2424### Step 2: Attributes
2525
26- For those who wish to use attributes with their entities, an additional package is available:
26+ To define a schema for Cycle ORM using PHP attributes, you can use the package [ cycle/annotated] ( https://github.com/cycle/annotated ) .
27+ Its current version requires ** PHP 8.1** . If you are using PHP 8.0, then version ** 3.x** of the package will be installed.
28+ In this case, you will need to refer to the configuration for this version below.
29+
30+ To install the package - execute:
2731
2832``` terminal
2933composer require cycle/annotated
@@ -102,12 +106,10 @@ Start by defining your first entity within the `src` directory of your project.
102106namespace:
103107
104108``` json composer.json
105- //...
106-
107109"autoload" : {
108- "psr-4" : {
109- "Example\\ " : " src/"
110- }
110+ "psr-4" : {
111+ "Example\\ " : " src/"
112+ }
111113}
112114```
113115
@@ -148,12 +150,6 @@ schema.
148150> ** Note:**
149151> Attributes won't degrade your application's performance. They are only used during the schema compilation process.
150152
151- To use attributes, you'll need to install the ` cycle/annotated ` package:
152-
153- ``` terminal
154- composer require cycle/annotated
155- ```
156-
157153Let's add attributes to our ` User ` entity:
158154
159155``` php src/User.php
@@ -222,10 +218,45 @@ print_r($classLocator->getClasses());
222218With the class locator set, you can piece together your schema compilation pipeline. This is the sequence through which
223219your entities pass, undergoing transformations, validations, and finally, integration into the ORM schema.
224220
221+ :::: tabs
222+
223+ ::: tab The current version
224+
225+ ``` php index.php
226+ use Cycle\Schema;
227+ use Cycle\Annotated;
228+ use Cycle\Annotated\Locator\TokenizerEmbeddingLocator;
229+ use Cycle\Annotated\Locator\TokenizerEntityLocator;
230+
231+ $embeddingLocator = new TokenizerEmbeddingLocator($classLocator);
232+ $entityLocator = new TokenizerEntityLocator($classLocator);
233+
234+ $schema = (new Schema\Compiler())->compile(new Schema\Registry($dbal), [
235+ new Schema\Generator\ResetTables(), // Reconfigure table schemas (deletes columns if necessary)
236+ new Annotated\Embeddings($embeddingLocator), // Recognize embeddable entities
237+ new Annotated\Entities($entityLocator), // Identify attributed entities
238+ new Annotated\TableInheritance(), // Setup Single Table or Joined Table Inheritance
239+ new Annotated\MergeColumns(), // Integrate table #[Column] attributes
240+ new Schema\Generator\GenerateRelations(), // Define entity relationships
241+ new Schema\Generator\GenerateModifiers(), // Apply schema modifications
242+ new Schema\Generator\ValidateEntities(), // Ensure entity schemas adhere to conventions
243+ new Schema\Generator\RenderTables(), // Create table schemas
244+ new Schema\Generator\RenderRelations(), // Establish keys and indexes for relationships
245+ new Schema\Generator\RenderModifiers(), // Implement schema modifications
246+ new Schema\Generator\ForeignKeys(), // Define foreign key constraints
247+ new Annotated\MergeIndexes(), // Merge table index attributes
248+ new Schema\Generator\SyncTables(), // Align table changes with the database
249+ new Schema\Generator\GenerateTypecast(), // Typecast non-string columns
250+ ]);
251+ ```
252+
253+ :::
254+
255+ ::: tab 3.x
256+
225257``` php index.php
226258use Cycle\Schema;
227259use Cycle\Annotated;
228- use Doctrine\Common\Annotations\AnnotationRegistry;
229260
230261$dbal = /** ... */;
231262
@@ -248,6 +279,10 @@ $schema = (new Schema\Compiler())->compile(new Schema\Registry($dbal), [
248279]);
249280```
250281
282+ :::
283+
284+ ::::
285+
251286In our example, we use ` SyncTables ` generator to automatically update the database schema. Read more about schema
252287synchronization in the [ Synchronizing Database Schema] ( ../advanced/sync-schema.md ) section.
253288
@@ -259,11 +294,10 @@ With the schema compiled, it can be integrated into the ORM:
259294
260295``` php index.php
261296use Cycle\ORM;
262- use Cycle\ORM\Schema;
263297
264298$schema = /** ... */;
265299
266- $orm = new ORM\ORM(new ORM\Factory($dbal), new Schema($schema));
300+ $orm = new ORM\ORM(new ORM\Factory($dbal), new ORM\ Schema($schema));
267301```
268302
269303It's best practice to cache the generated schema in production and regenerate it only when required, ensuring optimal
@@ -297,30 +331,29 @@ The mapping for `User` entity can be explicitly defined as:
297331
298332``` php index.php
299333use Cycle\ORM;
300- use Cycle\ORM\Schema;
301334use Cycle\ORM\Mapper\Mapper;
302335
303336$dbal = /** ... */;
304337
305- $orm = new ORM\ORM(new ORM\Factory($dbal), new Schema([
338+ $orm = new ORM\ORM(new ORM\Factory($dbal), new ORM\ Schema([
306339 'user' => [
307- Schema::MAPPER => Mapper::class, // default POPO mapper
308- Schema::ENTITY => User::class,
309- Schema::DATABASE => 'default',
310- Schema::TABLE => 'users',
311- Schema::PRIMARY_KEY => 'id',
340+ ORM\ Schema::MAPPER => Mapper::class, // default POPO mapper
341+ ORM\ Schema::ENTITY => User::class,
342+ ORM\ Schema::DATABASE => 'default',
343+ ORM\ Schema::TABLE => 'users',
344+ ORM\ Schema::PRIMARY_KEY => 'id',
312345
313346 // property => column
314- Schema::COLUMNS => [
347+ ORM\ Schema::COLUMNS => [
315348 'id' => 'id',
316349 'name' => 'name',
317350 ],
318351
319- Schema::TYPECAST => [
352+ ORM\ Schema::TYPECAST => [
320353 'id' => 'int',
321354 ],
322355
323- Schema::RELATIONS => [],
356+ ORM\ Schema::RELATIONS => [],
324357 ]
325358]));
326359```
0 commit comments