Welcome Guest, Not a member yet? Register   Sign In
Need help with a bridge table.
#3

[eluser]darkhouse[/eluser]
You're on the right track, at least your database looks to be setup correctly. Try this in your model:

Code:
function search($name){
   $this->db->select('c.*, s.name service_name');
   $this->db->join('services_bundles sb', 'sb.customer_id c.id');
   $this->db->join('services s', 's.id = sb.service_id');
   $this->db->like('c.name', $name);
   $query = $this->db->get('customers c');
   return $query->result();
}

So, if you run $your_model->search('Brian'), that will give you 2 records:
Code:
Array(
   [0] => Object(
      id => 1,
      name => Brian,
      service_name => Phone
   ),
   [1] => Object(
      id => 1,
      name => Brian,
      service_name => Internet
   )
)

Or, you could do it in 2 queries, like this:

Code:
function search($name){
   $this->db->like('name', $name);
   $query = $this->db->get('customers', 1);
   $row = $query->row();

   $this->db->join('services s', 's.id = sb.service_id');
   $this->db->where('sb.customer_id', $result->id);
   $query = $this->db->get('services_bundles sb');
   $row->services = $query->result();

   return $row;
}

So that would give you just 1 row, but an array of that customer's services also, like this:
Code:
Object(
   id => 1,
   name => Brian,
   services => Array(
      [0] => Object(
         id => 1,
         name => Phone
      ),
      [1] => Object(
         id => 2,
         name => Internet
      )
   )
)


Messages In This Thread
Need help with a bridge table. - by El Forum - 08-04-2009, 07:54 AM
Need help with a bridge table. - by El Forum - 08-04-2009, 08:43 AM
Need help with a bridge table. - by El Forum - 08-04-2009, 08:53 AM
Need help with a bridge table. - by El Forum - 08-05-2009, 08:17 AM
Need help with a bridge table. - by El Forum - 08-05-2009, 08:23 AM
Need help with a bridge table. - by El Forum - 08-05-2009, 09:18 AM
Need help with a bridge table. - by El Forum - 08-05-2009, 09:45 AM
Need help with a bridge table. - by El Forum - 08-07-2009, 09:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB