phpkohanakohana-3kohana-dbkohana-3.0

Kohana query builder


i am building query in kohana framework which looks like this:

DB::select('users.email')->from('users', 'roles_users')->
where('users.id', '=', 'roles_users.user_id')->
and_where('roles_users.role_id', '=', '2');

it would output:

SELECT `users`.`email` FROM `users`, `roles_users` 
WHERE `users`.`id` = 'roles_users.user_id' 
AND `roles_users`.`role_id` = '2'

so my problem is that kohana puts ' ' for me on roles_users.user_id so mysql searches for text not for actual value of the field. is there any way to solve it?


Solution

  • Is there a reason you are not joining the other table? Like that:

    DB::select('users.email')
    ->from('users')
    ->join('roles_users', 'LEFT')
    ->on('users.id', '=', 'roles_users.user_id')
    ->where('roles_users.role_id', '=', '2');
    

    If you don't like that, try

    DB::select('users.email')
    ->from('users', 'roles_users')
    ->where('users.id', '=', DB::expr('roles_users.user_id'))
    ->and_where('roles_users.role_id', '=', '2');