88use Doctrine \DBAL \Platforms \AbstractPlatform ;
99use Doctrine \DBAL \Schema \AbstractAsset ;
1010use Doctrine \DBAL \Schema \AbstractSchemaManager ;
11+ use Doctrine \DBAL \Schema \ColumnEditor ;
1112use Doctrine \DBAL \Schema \ComparatorConfig ;
1213use Doctrine \DBAL \Schema \DefaultExpression ;
1314use Doctrine \DBAL \Schema \DefaultExpression \CurrentDate ;
2122use Doctrine \DBAL \Schema \NamedObject ;
2223use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
2324use Doctrine \DBAL \Schema \Schema ;
25+ use Doctrine \DBAL \Schema \SchemaConfig ;
2426use Doctrine \DBAL \Schema \Table ;
2527use Doctrine \DBAL \Types \Types ;
2628use Doctrine \Deprecations \Deprecation ;
@@ -204,11 +206,32 @@ public function getSchemaFromMetadata(array $classes): Schema
204206 continue ;
205207 }
206208
207- $ table = $ schema ->createTable ($ this ->quoteStrategy ->getTableName ($ class , $ this ->platform ));
209+ // Create table object
210+ // @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
211+ if (method_exists (Schema::class, 'edit ' )) {
212+ $ table = new Table (
213+ $ this ->quoteStrategy ->getTableName ($ class , $ this ->platform ),
214+ [],
215+ [],
216+ [],
217+ [],
218+ [],
219+ $ metadataSchemaConfig ->toTableConfiguration (),
220+ );
221+ // Add default table options (charset, collation, engine, etc.)
222+ foreach ($ metadataSchemaConfig ->getDefaultTableOptions () as $ option => $ value ) {
223+ $ table ->addOption ($ option , $ value );
224+ }
225+ } else {
226+ $ table = $ schema ->createTable ($ this ->quoteStrategy ->getTableName ($ class , $ this ->platform ));
227+ }
208228
209229 if ($ class ->isInheritanceTypeSingleTable ()) {
230+ // For new schema API: collect join tables to add after this entity table
231+ $ joinTablesToAdd = [];
232+
210233 $ this ->gatherColumns ($ class , $ table );
211- $ this ->gatherRelationsSql ($ class , $ table , $ schema , $ addedFks , $ blacklistedFks );
234+ $ this ->gatherRelationsSql ($ class , $ table , $ schema , $ addedFks , $ blacklistedFks, $ metadataSchemaConfig , $ joinTablesToAdd );
212235
213236 // Add the discriminator column
214237 $ this ->addDiscriminatorColumnDefinition ($ class , $ table );
@@ -222,18 +245,37 @@ public function getSchemaFromMetadata(array $classes): Schema
222245 foreach ($ class ->subClasses as $ subClassName ) {
223246 $ subClass = $ this ->em ->getClassMetadata ($ subClassName );
224247 $ this ->gatherColumns ($ subClass , $ table );
225- $ this ->gatherRelationsSql ($ subClass , $ table , $ schema , $ addedFks , $ blacklistedFks );
248+ $ this ->gatherRelationsSql (
249+ $ subClass ,
250+ $ table ,
251+ $ schema ,
252+ $ addedFks ,
253+ $ blacklistedFks ,
254+ $ metadataSchemaConfig ,
255+ $ joinTablesToAdd ,
256+ );
226257 $ processedClasses [$ subClassName ] = true ;
227258 }
228259 } elseif ($ class ->isInheritanceTypeJoined ()) {
260+ // For new schema API: collect join tables to add after this entity table
261+ $ joinTablesToAdd = [];
262+
229263 // Add all non-inherited fields as columns
230264 foreach ($ class ->fieldMappings as $ fieldName => $ mapping ) {
231265 if (! isset ($ mapping ->inherited )) {
232266 $ this ->gatherColumn ($ class , $ mapping , $ table );
233267 }
234268 }
235269
236- $ this ->gatherRelationsSql ($ class , $ table , $ schema , $ addedFks , $ blacklistedFks );
270+ $ this ->gatherRelationsSql (
271+ $ class ,
272+ $ table ,
273+ $ schema ,
274+ $ addedFks ,
275+ $ blacklistedFks ,
276+ $ metadataSchemaConfig ,
277+ $ joinTablesToAdd ,
278+ );
237279
238280 // Add the discriminator column only to the root table
239281 if ($ class ->name === $ class ->rootEntityName ) {
@@ -253,7 +295,18 @@ public function getSchemaFromMetadata(array $classes): Schema
253295 $ this ->platform ,
254296 );
255297 // TODO: This seems rather hackish, can we optimize it?
256- $ table ->getColumn ($ columnName )->setAutoincrement (false );
298+ // Use new column editor API only when new schema editor API is available (DBAL 4.5+)
299+ // @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API for version detection)
300+ if (method_exists (Schema::class, 'edit ' )) {
301+ // New API: modify column using table editor (creates new table object)
302+ // This is safe because we'll add the table to schema later after all modifications
303+ $ table = $ table ->edit ()->modifyColumnByUnquotedName (
304+ $ columnName ,
305+ static fn (ColumnEditor $ column ) => $ column ->setAutoincrement (false ),
306+ )->create ();
307+ } else {
308+ $ table ->getColumn ($ columnName )->setAutoincrement (false );
309+ }
257310
258311 $ pkColumns [] = $ columnName ;
259312 $ inheritedKeyColumns [] = $ columnName ;
@@ -305,8 +358,19 @@ public function getSchemaFromMetadata(array $classes): Schema
305358 }
306359 }
307360 } else {
361+ // For new schema API: collect join tables to add after this entity table
362+ $ joinTablesToAdd = [];
363+
308364 $ this ->gatherColumns ($ class , $ table );
309- $ this ->gatherRelationsSql ($ class , $ table , $ schema , $ addedFks , $ blacklistedFks );
365+ $ this ->gatherRelationsSql (
366+ $ class ,
367+ $ table ,
368+ $ schema ,
369+ $ addedFks ,
370+ $ blacklistedFks ,
371+ $ metadataSchemaConfig ,
372+ $ joinTablesToAdd ,
373+ );
310374 }
311375
312376 $ pkColumns = [];
@@ -377,6 +441,23 @@ public function getSchemaFromMetadata(array $classes): Schema
377441
378442 $ processedClasses [$ class ->name ] = true ;
379443
444+ // Add the fully populated table to the schema
445+ // @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
446+ if (method_exists (Schema::class, 'edit ' )) {
447+ // @phpstan-ignore method.notFound (Using unreleased Schema::edit() API)
448+ $ schemaEditor = $ schema ->edit ();
449+ $ schemaEditor ->addTable ($ table );
450+ $ schema = $ schemaEditor ->create ();
451+
452+ // Add any join tables collected during relation processing
453+ // This ensures join tables appear right after their owning entity table
454+ foreach ($ joinTablesToAdd as $ joinTable ) {
455+ $ schemaEditor = $ schema ->edit ();
456+ $ schemaEditor ->addTable ($ joinTable );
457+ $ schema = $ schemaEditor ->create ();
458+ }
459+ }
460+
380461 if ($ class ->isIdGeneratorSequence () && $ class ->name === $ class ->rootEntityName ) {
381462 $ seqDef = $ class ->sequenceGeneratorDefinition ;
382463 $ quotedName = $ this ->quoteStrategy ->getSequenceName ($ seqDef , $ class , $ this ->platform );
@@ -614,15 +695,18 @@ private function gatherColumn(
614695 * fkOptions: array{onDelete?: string, deferrable?: bool, deferred?: bool}
615696 * }> $addedFks
616697 * @phpstan-param array<string, bool> $blacklistedFks
698+ * @phpstan-param list<Table> $joinTablesToAdd
617699 *
618700 * @throws NotSupported
619701 */
620702 private function gatherRelationsSql (
621703 ClassMetadata $ class ,
622704 Table $ table ,
623- Schema $ schema ,
705+ Schema & $ schema ,
624706 array &$ addedFks ,
625707 array &$ blacklistedFks ,
708+ SchemaConfig $ schemaConfig ,
709+ array &$ joinTablesToAdd ,
626710 ): void {
627711 foreach ($ class ->associationMappings as $ id => $ mapping ) {
628712 if (isset ($ mapping ->inherited ) && ! in_array ($ id , $ class ->identifier , true )) {
@@ -647,12 +731,29 @@ private function gatherRelationsSql(
647731 // create join table
648732 $ joinTable = $ mapping ->joinTable ;
649733
650- $ theJoinTable = $ schema ->createTable (
651- $ this ->quoteStrategy ->getJoinTableName ($ mapping , $ foreignClass , $ this ->platform ),
652- );
653-
654- foreach ($ joinTable ->options as $ key => $ val ) {
655- $ theJoinTable ->addOption ($ key , $ val );
734+ $ tableName = $ this ->quoteStrategy ->getJoinTableName ($ mapping , $ foreignClass , $ this ->platform );
735+
736+ // Create the join table object
737+ // @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
738+ if (method_exists (Schema::class, 'edit ' )) {
739+ $ theJoinTable = new Table (
740+ $ tableName ,
741+ [],
742+ [],
743+ [],
744+ [],
745+ $ joinTable ->options ,
746+ $ schemaConfig ->toTableConfiguration (),
747+ );
748+ // Add default table options (charset, collation, engine, etc.)
749+ foreach ($ schemaConfig ->getDefaultTableOptions () as $ option => $ value ) {
750+ $ theJoinTable ->addOption ($ option , $ value );
751+ }
752+ } else {
753+ $ theJoinTable = $ schema ->createTable ($ tableName );
754+ foreach ($ joinTable ->options as $ key => $ val ) {
755+ $ theJoinTable ->addOption ($ key , $ val );
756+ }
656757 }
657758
658759 $ primaryKeyColumns = [];
@@ -680,6 +781,13 @@ private function gatherRelationsSql(
680781 );
681782
682783 self ::addPrimaryKeyConstraint ($ theJoinTable , $ primaryKeyColumns );
784+
785+ // For new schema API: collect join table for deferred addition at end
786+ // For old schema API: table already added via createTable() above
787+ // @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
788+ if (method_exists (Schema::class, 'edit ' )) {
789+ $ joinTablesToAdd [] = $ theJoinTable ;
790+ }
683791 }
684792 }
685793 }
0 commit comments