cakephp / bake

The Bake Command Plugin
Other
112 stars 102 forks source link

Error bake while code generation #1012

Open luizcmarin opened 1 week ago

luizcmarin commented 1 week ago

Description

I am installing the "Authentication" plugin using the command "bin/cake bake model Users" (https://book.cakephp.org/authentication/3/en/index.html#building-a-login-action)

It returns the error "error: [Cake\Core\Exception\CakeException] Association alias Countries is already set. in...."

It turns out that I need these relationships.

This is a zero-scale application with only one table created so far, "tests". So I imported all the other tables. I installed the plugin and then ran the command mentioned. There is no call anywhere for the 'Users' table.

Suppot say: "i thought you get that error after you baked the code. but it happens while code generation."

Could you please check?

kancats.txt TestesTable.txt Testis.txt composer.txt Image Image Image

Bake Version

actual (see composer.txt)

PHP Version

833

ADmad commented 1 week ago

You have multiple foreign keys referencing the same table countries. That would require creating associations with a different alias than the default Countries. Bake is currently unable to handle such a case.

luizcmarin commented 1 week ago

But is it possible to implement this?

dereuromark commented 1 week ago

Yes We could detect non default relation and prefix automatically That is if there is already a country relation detectable.

other_country_id becomes OtherCountries alias. Etc

Want to try making a PR?

luizcmarin commented 1 week ago

Yes We could detect non default relation and prefix automatically That is if there is already a country relation detectable.

other_country_id becomes OtherCountries alias. Etc

Want to try making a PR?

I was unable to open a PR. Could you do it for me? I would be very grateful.

dereuromark commented 1 week ago

What did you try so far? How did you get the OtherCountries => Countries aliasing to work?

If autodetection doesnt work easily, we could consider allowing a Configure based mapping or alike to be read from with such a mapping. We dont have to put everything in Configure like others do ( https://github.com/CrestApps/laravel-code-generator/blob/master/config/default.php ), but we could allow at least some basic things to be more configurable along the way.

'Bake' => [
    'mapping' => [
        ...
    ],
],

or alike

We need the same for plugin => app aliasing anyway, since here the code is also currently broken and always puts PluginName.Users for example instead of Users (the latter is the only existing one).

luizcmarin commented 6 days ago

originally, cakephp 4, I had done it like this. Now I restarted the project with updated cakephp 5. (example with table 'users')

CONTROLLER:

public function view($id = null) { $user = $this->Users->get($id, [ 'contain' => ['UserA', 'UserB',...], ]);

$this->set(compact('user')); }

MODEL->ENTITY:

class User extends Entity { protected $_accessible = [ ... 'user_a' => true, 'user_b' => true, ... ];

MODEL->TABLE:

public function initialize(array $config): void { parent::initialize($config);

$this->addBehavior('Timestamp'); $this->addBehavior('Search.Search');

// para exibir auditoria na actions/view $this->belongsTo('UserA', [ 'className' => 'Users', 'foreignKey' => 'insert_by', ]); $this->belongsTo('UserB', [ 'className' => 'Users', 'foreignKey' => 'upated_by', ]); ...

luizcmarin commented 5 days ago

Maybe relation tableX->users = tableX->field-relation

tableX->insert_by, tableX->updated_by.... Etc

$user = $this->Users->get($id, [ 'contain' => ['tableX_updated_by', ...],

$this->belongsTo('tableX_updated_by')

dereuromark commented 2 days ago

With Cake5.1 bake I cannot reproduce a bug, the only existing relation is autoadded as expected: Countries The others are skipped, as such this is more a feature enhancement ticket now, to be able to auto guess also non default relations.

dereuromark commented 2 days ago

By the way: Looking into the code I found out that you can already also bake ShippingCountries and BillingCountries relations, using "references" in your schema (constraints).

//EDIT Now I see, I can reproduce this using such references. So my initial proposal still stands: Making the aliasing here happen before it tries to recreate the same association.

dereuromark commented 2 days ago

Maybe you want to continue with https://github.com/cakephp/bake/pull/1015