Welcome Guest, Not a member yet? Register   Sign In
Setting a value to null with Active Record
#1

[eluser]Unknown[/eluser]
Hi,

I'm trying to set a value to NULL in the database through ActiveRecord.
Code:
$this->db->set('phone_2',NULL);
$this->db->where('id',$uid);
$this->db->update('user');

Unfortunately, the set function in the active record module is defined like this.
So it ends up setting the value to the empty string instead of NULL.
Code:
function set($key, $value = '')

I guess I could write the whole query myself, but I would rather use active record.
Is there a way around this?

Thanks,
-Raymond
#2

[eluser]beatryder[/eluser]
[quote author="rkhalife" date="1193892216"]Hi,

I'm trying to set a value to NULL in the database through ActiveRecord.
Code:
$this->db->set('phone_2',NULL);
$this->db->where('id',$uid);
$this->db->update('user');

Unfortunately, the set function in the active record module is defined like this.
So it ends up setting the value to the empty string instead of NULL.
Code:
function set($key, $value = '')

I guess I could write the whole query myself, but I would rather use active record.
Is there a way around this?

Thanks,
-Raymond[/quote]


Sure
try this:
Code:
$this->db->set('phone_2','NULL'); //using the quotes usually works for me.
$this->db->where('id',$uid);
$this->db->update('user');
#3

[eluser]Unknown[/eluser]
[quote author="beatryder" date="1193909045"]
Sure
try this:
Code:
$this->db->set('phone_2','NULL'); //using the quotes usually works for me.
$this->db->where('id',$uid);
$this->db->update('user');
[/quote]

This won't work. It sets the value of phone_2 to the String 'NULL'.

Same as
Code:
$a = 'NULL';
and
Code:
$a = NULL;

are not the same thing.

-Raymond
#4

[eluser]beatryder[/eluser]
[quote author="rkhalife" date="1193957153"][quote author="beatryder" date="1193909045"]
Sure
try this:
Code:
$this->db->set('phone_2','NULL'); //using the quotes usually works for me.
$this->db->where('id',$uid);
$this->db->update('user');
[/quote]

This won't work. It sets the value of phone_2 to the String 'NULL'.

Same as
Code:
$a = 'NULL';
and
Code:
$a = NULL;

are not the same thing.

-Raymond[/quote]


You are correct, and I realized that this morning.

You might be able to do this:
Code:
$this->db->set('phone_2 = NULL',''); //passing an empty string causes active record to omit the "="
$this->db->where('id',$uid);
$this->db->update('user');

I haven't tested it, but it should work.
#5

[eluser]gtech[/eluser]
Beatryders solution does work.. This also worked for me

Code:
$this->db->where('id',$uid);
$this->db->update('user',array('phone_2' => NULL));
#6

[eluser]beatryder[/eluser]
Indeed that will do it.




Theme © iAndrew 2016 - Forum software by © MyBB