Welcome Guest, Not a member yet? Register   Sign In
Active record question
#1

[eluser]megabyte[/eluser]
So with CodeIgniter's active record, you can do this:


Code:
$this->db->set('id', $id, FALSE);

Add a third parameter.

But if you use an array:

Code:
$data = array('id' => $id);


How would you mimic this?
#2

[eluser]Shay Falador[/eluser]
Really simple:
Code:
$this->db->set($data, null, FALSE);

Just take a look at the method:
Code:
function set($key, $value = '', $escape = TRUE)
    {
        $key = $this->_object_to_array($key);
    
        if ( ! is_array($key))
        {
            $key = array($key => $value);
        }    

        foreach ($key as $k => $v)
        {
            if ($escape === FALSE)
            {
                $this->ar_set[$this->_protect_identifiers($k)] = $v;
            }
            else
            {
                $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
            }
        }
        
        return $this;
    }
#3

[eluser]megabyte[/eluser]
What I'm saying is that the only way to not escape data is using the set method.

You can't set it to not escape a certain value if you do this:

Code:
$data = array(
               'title' => 'My title' ,
               'name' => 'My Name' ,
               'id' => 7
            );

$this->db->insert('mytable', $data);

Its still going to escape the id

Am I right?
#4

[eluser]Shay Falador[/eluser]
Right.
Insert was not really designed for this... It takes only 2 arguments...
#5

[eluser]Jason Tan Boon Teck[/eluser]
I'm trying to build an insert statement, using set. For example, in the model:

Code:
...
if(isset($data_array['contact_type'])){$this->db->set('contact_type', $data_array['contact_type']);}
if(isset($data_array['start_date'])){$this->db->set('start_date', $data_array['start_date']);}
if(isset($data_array['contact_address'])){$this->db->set('address', $data_array['contact_address']);}
...

And this is the result

Code:
A Database Error Occurred

Error Number:

ERROR: syntax error at or near "Jalan" LINE 1: ...011120001193888406, 'Residence', 2007-11-01, 3258 Jalan 18/3... ^

INSERT INTO "contact_info" ("contact_id", "person_id", "contact_type", "start_date", "address", "address_2", "address_3", "town", "state", "postcode", "tel_home", "tel_office", "tel_mobile", "fax_no", "email", "synch_out")
VALUES (1267089875, 13051946011120001193888406, 'Residence', 2007-11-01, 3258 Jalan 18/37, Kampung Cempaka, , Petaling Jaya, Selangor, 46000, 03-42563733, , 012-2281996, , , 1148691352)

Somehow, some string values were not composed inside quotes, e.g. address, address_2, causing PostgreSQL to choke. What is the proper way to ensure each string item is in quotes?




Theme © iAndrew 2016 - Forum software by © MyBB