I'm working on a CodeIgniter project that uses grocery-crud.
I'm using this code:
$crud->set_table('dtr');
$crud->set_relation_n_n('branch', 'users_branches', 'branches', 'user_id', 'branch_id', 'name');
In SQL, its equivalent is this:
SELECT `dtr`.*, (SELECT GROUP_CONCAT(DISTINCT branches.name)
FROM branches
LEFT JOIN users_branches ON users_branches.branch_id = branches.id WHERE
>> users_branches.user_id = `dtr`.id
GROUP BY users_branches.user_id) AS branch FROM (`dtr`)
But what I want to happen is this:
SELECT `dtr`.*, (SELECT GROUP_CONCAT(DISTINCT branches.name) FROM branches LEFT JOIN users_branches ON users_branches.branch_id = branches.id WHERE
users_branches.user_id = `dtr`.user_id
GROUP BY users_branches.user_id) AS branch FROM (`dtr`)
I cannot make it happen the way I need it to because table dtr's primary key is 'id' and not 'user_id'. Besides, changing the primary key would cause a lot of error on other parts of my system. So I need to find a way to code it differently so it would work right.
Please help. Thanks.