feat: add Many-to-Many relationship between Projects and Users - #21
Merged
Conversation
MarczalTSIGP
reviewed
Dec 2, 2025
| $projectId = (int)$params['id']; | ||
| $userIdToAdd = (int)$params['user_id']; | ||
|
|
||
| $project = Project::findById($projectId); |
There was a problem hiding this comment.
Permite que o usuário logado adicione membros em qualquer projeto, até naqueles não pertencem a ele.
| $projectId = (int)$params['id']; | ||
| $userIdToRemove = (int)$params['user_id']; | ||
|
|
||
| $project = Project::findById($projectId); |
There was a problem hiding this comment.
Permite remover membros de qualquer projeto do sistema.
| <?php foreach ($teamMembers as $member): ?> | ||
| <li class="list-group-item d-flex justify-content-between align-items-center"> | ||
| <div> | ||
| <strong><?= htmlspecialchars($member->name) ?></strong> |
There was a problem hiding this comment.
Qual a necessidade de htmlspecialchars?
Comment on lines
+97
to
+112
| <?php if ($this->isAdmin()): ?> | ||
| <hr> | ||
| <h5 class="card-title mt-3">Adicionar Membro à Equipe</h5> | ||
|
|
||
| <form action="<?= route('projects.addMember', ['id' => $project->id]) ?>" method="POST" class="row g-3 align-items-end"> | ||
| <div class="col-md-8"> | ||
| <label for="user_id" class="form-label">Selecionar Pesquisador:</label> | ||
| <select name="user_id" id="user_id" class="form-select" required> | ||
| <option value="">-- Escolha um usuário --</option> | ||
| <?php foreach ($allUsers as $user): ?> | ||
| <option value="<?= $user->id ?>"> | ||
| <?= htmlspecialchars($user->name) ?> (<?= htmlspecialchars($user->email) ?>) | ||
| </option> | ||
| <?php endforeach; ?> | ||
| </select> | ||
| </div> |
There was a problem hiding this comment.
Apenas esconde a view, mas não protege o backend!
There was a problem hiding this comment.
Adicionar ao .gitignore ./public/assets/images/*
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This Pull Request implements the Many-to-Many (NxN) relationship between Projects and Users, allowing the creation of research teams. Now, a Project can have multiple Members, and a User can participate in multiple Projects.
This implementation covers the full stack: Database schema, Core Framework improvements, MVC logic, UI integration, and Automated Tests.
Changes Implemented
Created the pivot table project_user with foreign keys and ON DELETE CASCADE.
Added a UNIQUE constraint to prevent duplicate members in the same team.
Feature Upgrade: Enhanced the BelongsToMany class in the Core.
Added attach(int $id) method to handle INSERT operations on pivot tables.
Added detach(int $id) method to handle DELETE operations on pivot tables.
Model: Added the team() method to the Project model using the belongsToMany relationship.
Controller: Implemented addMember and removeMember methods in ProjectsController with authentication checks.
View: Updated projects/show.phtml to list team members and provide "Add/Remove" controls.
Added Unit Tests for the Project model integration.
Added Unit Tests specifically for the BelongsToMany core class logic.
Updated the Populate script (ProjectUserPopulate) to automatically seed team associations.
How to Test
Setup Database:
Bash
./run db:reset
./run db:populate
Verify that projects now have team members assigned automatically.
Run Automated Tests:
Bash
./run test
Expected result: All tests passed (Green).
Manual Testing (UI):
Go to a Project details page.
Use the "Adicionar Membro à Equipe" section to select a user and add them.
Verify the user appears in the list.
Click "Remover Membro" and verify the user is removed.
Try adding the same user twice (System should prevent duplicates).