Great question! When using Grocery CRUD's setRelationNtoN, filtering the related records based on the current user or some other condition requires a bit of customization because by default, setRelationNtoN pulls all related records without filtering.
Your scenario recap:
- You have Contractors, Projects, and a Contractors-Projects relation table.
- You also have a Project-Company relation table that controls which contractors have access to which projects.
- You want to filter the projects shown in the N-to-N relation based on the current user (contractor) and their company access.
How to implement filtering on setRelationNtoN in Grocery CRUD
Step 1: Use setRelationNtoN with a callback to filter the related records
Grocery CRUD allows you to customize the query used to fetch the related records by using the setRelationNtoN method with a callback function.
$crud->setRelationNtoN(
'projects', // field name
'contractors_projects', // relation table
'projects', // related table
'contractor_id', // primary key of current table in relation table
'project_id', // primary key of related table in relation table
'project_name', // field to show
function ($query) use ($current_user_id) {
// $query is a CI_DB_query_builder instance
// Join with project-company table to filter projects by company access
$query->join('project_company', 'project_company.project_id = projects.id');
$query->where('project_company.company_id IN (SELECT company_id FROM contractors_companies WHERE contractor_id = ' . intval($current_user_id) . ')');
return $query;
}
);
Explanation:
- The last parameter of
setRelationNtoN can be a callback function that receives the query builder object.
- You join the
project_company table to filter projects based on the companies the contractor has access to.
- You filter projects by checking if the
company_id is in the list of companies assigned to the current contractor.
$current_user_id should be the ID of the logged-in contractor (you can get it from your session or auth system).
Step 2: Get the current user ID
Make sure you have the current user ID available in your controller:
$current_user_id = $this->session->userdata('user_id'); // or however you get the logged-in user
Step 3: Adjust table and field names
Make sure to replace the table and field names with your actual database schema:
contractors_projects = your N-to-N relation table
project_company = table linking projects to companies
contractors_companies = table linking contractors to companies (if you have one)
projects.id and projects.project_name = your project table primary key and display field
Alternative: Use setRelationNtoN with a custom model method
If the callback approach is not flexible enough, you can override the model method that fetches the related records, but the callback is usually sufficient.
Summary
- Use the callback parameter in
setRelationNtoN to customize the query.
- Join the necessary tables to filter projects based on the current user's company access.
- Pass the current user ID to the callback.
- Return the modified query builder.
If you want, I can help you write the exact code snippet tailored to your database schema. Just share your table and field names!