Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -14,6 +15,7 @@
* @since 0.1.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Bake\Command;

use Bake\CodeGen\FileBuilder;
Expand Down Expand Up @@ -275,6 +277,9 @@ public function applyAssociations(Table $model, array $associations): void
if (get_class($model) !== Table::class) {
return;
}

$this->ensureAliasUniqueness($associations);

foreach ($associations as $type => $assocs) {
foreach ($assocs as $assoc) {
$alias = $assoc['alias'];
Expand Down Expand Up @@ -382,16 +387,19 @@ public function findBelongsTo(Table $model, array $associations, ?Arguments $arg
}
$assoc = [
'alias' => $tmpModelName,
'className' => $tmpModelName,
'foreignKey' => $fieldName,
];
if ($schema->getColumn($fieldName)['null'] === false) {
$assoc['joinType'] = 'INNER';
}
}

if ($this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
if (!empty($assoc['className'])) {
$assoc['alias'] = $assoc['className'] . '_' . $model->getAlias() . '_' . $fieldName;
}
$associations['belongsTo'][] = $assoc;
}

Expand Down Expand Up @@ -708,7 +716,7 @@ public function getEntityPropertySchema(Table $model): array
if ($entityClass === '\Cake\ORM\Entity') {
$namespace = Configure::read('App.namespace');

[$plugin, ] = pluginSplit($association->getTarget()->getRegistryAlias());
[$plugin,] = pluginSplit($association->getTarget()->getRegistryAlias());
if ($plugin !== null) {
$namespace = $plugin;
}
Expand Down Expand Up @@ -1352,7 +1360,7 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
])->addOption('skip-relation-check', [
'boolean' => true,
'help' => 'Generate relations for all "example_id" fields'
. ' without checking the database if a table "examples" exists.',
. ' without checking the database if a table "examples" exists.',
])->setEpilog(
'Omitting all arguments and options will list the table names you can generate models for.'
);
Expand Down Expand Up @@ -1546,4 +1554,41 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console
$enumCommand->execute($args, $io);
}
}

/**
* @param array<string, array<string, mixed>> $associations
* @return array<string, array<string, mixed>>
*/
protected function ensureAliasUniqueness(array $associations): array
{
$existing = [];
foreach ($associations as $type => $associationsPerType) {
foreach ($associationsPerType as $k => $association) {
$alias = $association['alias'];
if (in_array($alias, $existing, true)) {
$alias = $this->alias($association);
}
$existing[] = $alias;
if (empty($association['className'])) {
$className = $this->plugin ? $this->plugin . '.' . $association['alias'] : $association['alias'];
$association['className'] = $className;
}
$association['alias'] = $alias;
$associations[$type][$k] = $association;
}
}

return $associations;
}

/**
* @param array<string, mixed> $association
* @return string
*/
protected function alias(array $association): string
{
$foreignKey = $association['foreignKey'];

return $this->_modelNameFromKey($foreignKey);
}
}