Adopt Eloquent model toolset for relationship tables #8103
Replies: 6 comments 13 replies
This comment has been hidden.
This comment has been hidden.
-
It looks like you've given this |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
Am I reading that right that UserUserGroup::withUserId(123); |
Beta Was this translation helpful? Give feedback.
-
Can you give some examples? I think the killer feature here is eager loading, but looking at the code above it's not clear to me how that would work if the getter methods always call the Assuming that |
Beta Was this translation helpful? Give feedback.
-
@asmecher Why not fill the property |
Beta Was this translation helpful? Give feedback.
-
Motivation
Currently we don't have a good, consistent, modern pattern for implementing relationship tables in OJS/OMP/OPS.
Relationship tables are tables that contain foreign keys to relate to 2 or more other tables and do not have their own autoincrementing primary keys (yet). Here are some simple examples:
user_user_groups
(relatesusers
touser_groups
)email_log_users
(relatesemail_log
tousers
)publication_categories
(relatespublications
tocategories
)query_participants
(relatesqueries
tousers
)review_files
(relatesreview_assignments
tosubmission_files
)user_interests
(relatesusers
tocontrolled_vocab_entries
)There are some others that probably qualify but are somewhat unusual:
submission_file_revisions
(relates more than 2 tables)subeditor_submission_group
(relates more than 2 tables; kind of has its own DAO:classes/context/SubEditorsDAO.inc.php
)submission_search_object_keywords
(includes a "position" column)user_group_stage
(relates more than 2 tables)Currently these are implemented by jamming the functions creating/removing/updating their entries into the
DAO
class of one of the "parent" entities. For example, functions maintainingpublication_categories
entries are inlib/pkp/classes/category/DAO.inc.php
; this is arbitrary as they could just as well be inlib/pkp/classes/publication/DAO.inc.php
.Goals
I propose that we...
lib/pkp/Domains/Jobs/FailedJob.php
andlib/pkp/Domains/Jobs/Job.php
.) This may prove whether/how to adopt it further in the future.Proposal
Use Eloquent models to represent/maintain relationship tables.
Example model class for
user_user_groups
:Calling code
Queries
Creation
Insert a new row (and return the model object):
Single entry update
Batch update
🛑 This is a standard formulation but requires calling code to know column names!
We rarely use this kind of query, but when we have to, a local scope containing an
update
call can be used to avoid exposing column names. See e.g. this Medium article for an example.Pros/Cons
Pros:
Cons:
Beta Was this translation helpful? Give feedback.
All reactions