4spacesdk / CI4OrmExtension

OrmExtension for CodeIgniter 4
MIT License
50 stars 9 forks source link

RelationDefjoinTable is wrong #13

Closed Noext closed 3 years ago

Noext commented 3 years ago

Hello

when using the example in the readme ( users, roles, colors ), the cache generated for the relations between users and colors is wrong, RelationDefjoinTable is set to 'color_users' where it should be empty, there is no need to have a joint table here.

this result is an error when you want to get all users of a color, the SQL builder will look for the table 'color_users' instead of the color_id in the users table

Martin-4Spaces commented 3 years ago

Hi @Noext,

Thank you for pointing this out. Can you tell me which CI4OrmExtension version you use?

Noext commented 3 years ago

hello, i'm using the latest version : 1.0.1

Martin-4Spaces commented 3 years ago

I cannot reproduce what you are describing.

It sounds like your users table does not have a color_id field. If it does, then try to clear the OrmExtension cache by deleting the related files from writable/cache. And then try again. This could happen if the cache was build before you added the field to the users table.

Martin-4Spaces commented 3 years ago

You can find a working example here https://github.com/4spacesdk/CI4-RestOrmExtensionSample/tree/issues/13.

Noext commented 3 years ago

I'm using the SQL you provided in the README

seems the error is coming from DataMapper/RelationDef.php line 81 where it always set a join table equal to "modelTable_relationClassName" even if the relationship is not made by a third table

for now the only fix i have found is to declare my relation in ColorModel like this public $hasMany = [ UserModel::class => [ "class" => UserModel::class, "otherField" => ColorClass::class, "joinSelfAs" => "color_id", "joinOtherAs" => "user_id", "cascadeDelete" => true, "joinTable" => 'users' ] ];

this prevent the usage for the "color_users" table

Martin-4Spaces commented 3 years ago

Have you tried resetting the OrmExtension cache? QueryBuilder will use this method to find the relationship table. In this example it will hit the first case. But it does not seem like it in your case. If you cleared the OrmExtension cache, then look for the response of getTableFields. It must include role_id to work.

ci4/vendor/4spacesdk/ci4ormextension/DataMapper/RelationDef.php CI4-RestOrmExtensionSample_–___PhpstormProjects_CI4-RestOrmExtensionSample_ci4_vendor_4spacesdk_ci4ormextension_DataMapper_RelationDef_php