Welcome Guest, Not a member yet? Register   Sign In
Problem with double join the same table with Active Record
#1

[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?
#2

[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));




Theme © iAndrew 2016 - Forum software by © MyBB