Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luizmarin 1 #1016

Closed
wants to merge 3 commits into from
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,5 +1,6 @@
<?php

declare(strict_types=1);

Check failure on line 3 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Expected 0 lines before declare statement, found 1.

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
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 @@
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 @@
}
$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;

Check failure on line 401 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Double space found
}
$associations['belongsTo'][] = $assoc;
}

Expand Down Expand Up @@ -708,7 +716,7 @@
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 @@
])->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 @@
$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);
}
}
Loading