2

I have this following query for CodeIgniter:

$q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")')
            ->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")')
            ->get('messages');

I want to write this query with completely active record.

I have tried something like this:

$from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']);
            $to_where   = array('message_from'=>$this->auth_model->userdata['user_id'],'message_to'=>$user_id);    
            $q = $this->db->where($from_where)
            ->or_where($to_where)
            ->get('messages');

            die($this->db->last_query());

The above code produces this query:

SELECT * FROM (`messages`) WHERE `message_from` = '2' AND `message_to` = '1' OR `message_from` = '1' OR `message_to` = '2'

But this is what I want to produce:

SELECT * FROM (`messages`) WHERE (message_from="2" AND message_to="1") OR (message_from="1" AND message_to="2")

There are similar questions here and here, but thosedid not provide a real solution for me.

How's this possible, If not via core libraries, is there an extension which allows writing such queries?

Thanks,

Community
  • 1
  • 1
Arda
  • 5,635
  • 2
  • 44
  • 65

1 Answers1

1

You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions

public function _compile_select($select_override = FALSE)
public function _reset_select()

Now subquery writing in available And now here is your query with active record

$this->db->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();


$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("$subQuery1");
$this->db->or_where("$subQuery2");
$this->db->get('messages');

Look at this answer of mine. This shows how to use sub queries. This will help
Using Mysql WHERE IN clause in codeigniter

EDITES

Yes i have done it Rewrite the query this way exactly you want

$this->db->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();


$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("($subQuery1)");
$this->db->or_where("($subQuery2)");
$this->db->get('messages');

Compile select is with true parameter. Will not produce select clause. This will produce

SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')
Community
  • 1
  • 1
Muhammad Raheel
  • 19,442
  • 6
  • 62
  • 98
  • Thanks, I also have thought about using subqueries back then, but gave it up because of speed concerns. Would using subqueries will slow down the process a lot? – Arda Jun 25 '12 at 09:51
  • this i haven't tested try to use sub queries where the whole query is required for only where condition i haven't tested it yet.if subquery is dependant it will slow down the process insted join will do things faster – Muhammad Raheel Jun 25 '12 at 09:55