4spacesdk/CI4OrmExtension

RelationDefjoinTable is wrong

Closed this issue · 6 comments

Noext commented

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

Hi @Noext,

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

Noext commented

hello, i'm using the latest version : 1.0.1

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.

Noext commented

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

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