Welcome Guest, Not a member yet? Register   Sign In
Quick Question - DB - Active Record and NULL values
#1

[eluser]MEM[/eluser]
Hello,


I had this working:
Code:
$query=$this->db->get_where('category', array('parent_id_cat'=>NULL));

I wanted to add more where clauses on it:
Code:
$this->db->where('activa_cat =', 1);
$this->db->where('parent_id_cat =', NULL);

But this last line gives me an error. He is not accepting the NULL param.


What am I doing wrong?


Regards,
Márcio
#2

[eluser]bretticus[/eluser]
[quote author="MEM" date="1251573840"]Hello,


I had this working:
Code:
$query=$this->db->get_where('category', array('parent_id_cat'=>NULL));


But this last line gives me an error. He is not accepting the NULL param.


What am I doing wrong?[/quote]

If the first form works, then add another element to your array. Otherwise, you can tell Active Record not to meddle with the where clause statement:

Code:
$this->db->where('parent_id_cat IS NULL', NULL, FALSE);
#3

[eluser]MEM[/eluser]
[quote author="bretticus" date="1251574914"]
If the first form works, then add another element to your array.[/quote]
Yes, but the fact that I wanted to use get->where() instead, was to avoid a large array when I have more then 3 or 4 where clauses.


[quote author="bretticus" date="1251574914"]Otherwise, you can tell Active Record not to meddle with the where clause statement:

Code:
$this->db->where('parent_id_cat IS NULL', NULL, FALSE);
[/quote]

If I get you right,
By using IS NULL instead of = NULL - AND - by passing values to the others Where() method parameters, I'm telling Active Record to not mess with where statement?


Thanks for your reply,
Márcio
#4

[eluser]MEM[/eluser]
Ok, I believe is solved:

Instead of:
Code:
$this->db->where('parent_id_cat =', NULL);

We can take out the = sign:
Code:
$this->db->where('parent_id_cat', NULL);

Regards,
Márcio
#5

[eluser]bretticus[/eluser]
[quote author="MEM" date="1251576376"]


[quote author="bretticus" date="1251574914"]
Code:
$this->db->where('parent_id_cat IS NULL', NULL, FALSE);
[/quote]

If I get you right,
By using IS NULL instead of = NULL - AND - by passing values to the others Where() method parameters, I'm telling Active Record to not mess with where statement?
Márcio[/quote]

IS NULL is noting more than my preference but making the last parameter of where() FALSE is where is where you tell AR not to mes with the clause you send.

Quote:$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

Code:
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
#6

[eluser]MEM[/eluser]
I didn't know about that backticks option.
I still need to properly understand how the use of '' can protect or meddle with our queries. But that's a question I should solve outside CI scope.

Thanks for your time,
Márcio




Theme © iAndrew 2016 - Forum software by © MyBB