Welcome Guest, Not a member yet? Register   Sign In
Backticks option for $this->db->insert() with array() format?
#1

[eluser]Ninjabear[/eluser]
The set() function has a backticks option, so you can go:

$this->db->set('date_added','now()',FALSE); and it will come out as insert into mytable('date_added') values(now()).

This is very handy but it only seems to be present on the set() function and now when using $this->db->insert('table',array()) option.

So my question is, is there a way of getting to the backticks with this syntax? Recently I built a function to get around this option as so:

Code:
$this->_set_default(array('date_added'=>'now()','author'=>$this->session->userdata('userId') ),
                      $options,
            array('date_added'=>FALSE));

            $this->db->insert($this->_table);

  /*
   * _set_default method takes defaults and combines them with what was entered into options. It also takes a third param which supports removal of backticks for each param if necessary.
  * This function will always return true.
  * -----------------------------
  *  
  */  
  function _set_default( $defaults=array(),$options=array(),$backticks=array() )
  {
    $merged = array_merge($defaults,$options);
  
   foreach($merged as $k=>$v):
    if( isset($backticks[$k]) )
     $this->db->set($k,$v,FALSE);
    else
     $this->db->set($k,$v);
   endforeach;
  
   return true;
  }

It works great but I wonder if it's built in but not documented?
#2

[eluser]Aken[/eluser]
No it is not currently available as a default option. You'd need to add on that functionality, like you have.
#3

[eluser]Ninjabear[/eluser]
[quote author="Aken" date="1330997802"]No it is not currently available as a default option. You'd need to add on that functionality, like you have.[/quote]

Cool, glad I made something useful for myself. P.s. I have a couple of questions about the code I wrote:

1. Should I make the return statement true/false, if so on what condition?
2. I had to use the set function here which will only be relevant for the next query. Once that's executed the set() will be deleted I assume. Is this the best way of achieving this?
#4

[eluser]Aken[/eluser]
1) You don't need to return anything unless the code that calls the function is expecting a returned value. In your case, you don't need to return anything.

2) That's how I would handle it. CI's active record is designed to reset after queries are called. If you don't want certain properties to be reset, you'll have to modify the code, and you'll also have to remember to reset the queries on your own where necessary.




Theme © iAndrew 2016 - Forum software by © MyBB