CodeIgniter Forums
Is it possible with Active Record? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Is it possible with Active Record? (/showthread.php?tid=64670)



Is it possible with Active Record? - fulopm - 03-19-2016

Hello!

I have this embedded db query, and I want to rewrite it in Active Record-style:

PHP Code:
 $sql "SELECT id, fullname, phone FROM users WHERE country=(SELECT country_id FROM country WHERE name=?) AND  type=?"
Is it possible? If so, can you please tell me how can I start with it? Smile

Thank you!


RE: Is it possible with Active Record? - arma7x - 03-19-2016

$this->db->select('user.id,user.fullname,user.phone');
$this->db->from('user');
$this->db->join('country', 'country.country_id = user.country_id');
$this->db->where('country.name' , $name);
$this->db->where('user.type' , $type);
$query = $this->db->get();
Can you list all column/field of user & country table. My assumption is find user based on their country & user role.


RE: Is it possible with Active Record? - ardhie1032 - 03-19-2016

(03-19-2016, 11:39 AM)fulopm Wrote:
PHP Code:
 $sql "SELECT id, fullname, phone FROM users WHERE country=(SELECT country_id FROM country WHERE name=?) AND  type=?"

Simple with query bindings (without AR or QB):
PHP Code:
$sql "SELECT id, fullname, phone FROM users WHERE country=(SELECT country_id FROM country WHERE name=?) AND  type=?";
$this->db->query($sql, array($name$type)); 

Query Bindings


RE: Is it possible with Active Record? - fulopm - 03-19-2016

Thank you @arma7x, you helped a lot with understanding of joins! Now I can rewrite it with active record. Smile


RE: Is it possible with Active Record? - arma7x - 03-20-2016

You're welcome, did you still using v2.x.x. Please upgrade to ver 3.0.5.


RE: Is it possible with Active Record? - fulopm - 03-21-2016

(03-20-2016, 03:16 PM)arma7x Wrote: You're welcome, did you still using v2.x.x. Please upgrade to ver 3.0.5.

What made you think that I'm using 2.x.x? :O I'm using 3.0.5 (from now on, 3.0.6).


RE: Is it possible with Active Record? - arma7x - 03-21-2016

Due to
3.0 .0's renaming of Active Record to Query Builder