Welcome Guest, Not a member yet? Register   Sign In
ActiveRecord WHERE clause joins
#1

[eluser]Jakobud[/eluser]
I have two basic database tables like so:

Code:
table: clients
columns: id, name

table: projects
columns: id, name, client_id

This is the MySQL query I want to run:

Code:
SELECT projects.id, projects.name, clients.name
FROM projects, clients
WHERE projects.client_id = clients.id

I'm trying to figure out how to do this with ActiveRecord. I tried this:

Code:
$this->db->select('projects.id, projects.name, clients.name');
$this->db->from('projects, clients');
$this->db->where('projects.client_id = clients.id');
$this->db->get();

This seems to work, however it only returns projects.id and clients.name. It's like it ignores projects.name since it shares the same column name as clients.name. Also it turns those column names as 'id' and 'name' instead of 'projects.id' and 'clients.name'.

Is there an easier way to write this that I'm just not seeing in the User Guide or Wiki?
#2

[eluser]Jan_1[/eluser]
more alike this:
Code:
$this->db->select('projects.id,
                   projects.client_id,
                   projects.name as project,
                   clients.id,
                   clients.name as client
                  ');
$this->db->from('projects');
$this->db->join('clients', 'clients.id = projects.client_id');
$query = $this->db->get();
have a try
#3

[eluser]Jakobud[/eluser]
Ah okay. Now that I think about it that was kinda obvious. I just wasn't sure if there was any better way of writing it. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB