CodeIgniter Forums
Problem with double join the same table with Active Record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Problem with double join the same table with Active Record (/showthread.php?tid=56348)



Problem with double join the same table with Active Record - El Forum - 12-12-2012

[eluser]amatotax[/eluser]
Hello ppl, I've tried diffrent solutions, noone works fine :/


My query:

Code:
public function get_accepted_relations($uid)
{
$this->db
->select('users.id uid, relations.*')
->from('users')
->join('relations', 'relations.sender = users.id')
->where(array('users.id' => $uid, 'is_accepted' => 1));

$query = $this->db->get();
if($query->num_rows() > 0)
{
  return $query->result();
}
else
{
  return FALSE;
}
}

If I put

Code:
->join('relations', 'relations.recipient = users.id')

after first JOIN I'm gettin the following error:

Quote:Error Number: 1066

Not unique table/alias: 'relations'

SELECT `users`.`id` uid, `relations`.* FROM (`users`) JOIN `relations` ON `relations`.`sender` = `users`.`id` JOIN `relations` ON `relations`.`recipient` = `users`.`id` WHERE `users`.`id` = 1 AND `is_accepted` = 1

Filename: /var/www/models/mysite_model.php

Line Number: 89

Even
Code:
->join('relations', 'relations.recipient = users.id', 'LEFT')
seems not working
Without the second JOIN it works perfect! Any ideas dear CI users how to double join the same table?


Problem with double join the same table with Active Record - El Forum - 12-12-2012

[eluser]amatotax[/eluser]
Youupiee made it Big Grin

Code:
$this->db
->select('users.id, rel.sender, rel.recipient')
->from('users')
->join('relations as rel', 'rel.sender = users.id OR rel.recipient = users.id')
->where(array('users.id' => $uid, 'rel.is_accepted' => 1));