1

I'm trying to make a multiple conditional join using mysql function to make it, but codeigniter place function between "`" and fail the request.

$this->db
          ->join(
            'contribution_contributions t2',
            $this->db->dbprefix($this->_table).'.id = t2.contact_id  AND `t2`.`created` >= NOW() - INTERVAL 5 DAY AND `t2`.`created` < (NOW() +
                    INTERVAL 5 DAY',
            'inner'
          );

The query

INNER JOIN `default_contribution_contributions` `t2` ON `default_contribution_contacts`.`id` = `t2`.`contact_id` AND `NOW`() - `INTERVAL 5` `DAY` AND `NOW`() + `INTERVAL 5` `DAY)`

The string t2.created disappeared and both condition are placed between "`"

`NOW`() - `INTERVAL 5` `DAY` AND `NOW`() + `INTERVAL 5` `DAY)`
Cœur
  • 37,241
  • 25
  • 195
  • 267
abenevaut
  • 768
  • 1
  • 9
  • 23
  • 1
    The `select()` method apparently has a parameter to set `false` and disable identifier quoting. http://stackoverflow.com/questions/7482594/weird-backticks-behaviour-in-active-record-in-code-igniter-2-0-3 Not sure if `join()` has similar functionality, but assuming you have or could have a `select()` in the query perhaps you could try that. – Michael Berkowski Jan 17 '15 at 13:16
  • 1
    I don't know CI well and can't test. I won't close as a duplicate since I don't know if this applies to joins the same way. Reading [the docs](https://ellislab.com/codeigniter/user-guide/database/active_record.html#select) though, you could replace the entire expression with a `->select()` if you're willing to give up some of the activerecord behavior. – Michael Berkowski Jan 17 '15 at 13:20
  • Yes! join() have an argument too! – abenevaut Jan 17 '15 at 13:28
  • Ok, I'll link this as a duplicate to the other one then. It's unfortunate that CI's documentation doesn't actually list all the arguments and you have to go into the source to find these things. – Michael Berkowski Jan 17 '15 at 13:31
  • You should post an answer below. I won't do the duplicate linking until you do. – Michael Berkowski Jan 17 '15 at 13:31

1 Answers1

2

Last join argument allow to skip escape condition with "`"

/**
 * Join
 *
 * Generates the JOIN portion of the query
 *
 * @param   string
 * @param   string  the join condition
 * @param   string  the type of join
 * @param   string  whether not to try to escape identifiers
 * @return  object
 */
public function join($table, $cond, $type = '', $escape = NULL)

Thanks Michael!

abenevaut
  • 768
  • 1
  • 9
  • 23