Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

looking for NULL fields uses equal sign instead of IS #134

Closed
greut opened this issue Mar 18, 2011 · 2 comments
Closed

looking for NULL fields uses equal sign instead of IS #134

greut opened this issue Mar 18, 2011 · 2 comments

Comments

@greut
Copy link
Collaborator

greut commented Mar 18, 2011

I realised that PHP AR wasn't handling WHERE correctly while looking for NULL fields doing = instead of IS.

And MySQL doesn't like that, Problems with NULL Values:
“To look for NULL values, you must use the IS NULL test.”

Find here a little patch, because I'm too lazy to branch and send a pull request that (you'll cherry pick anyway).

Cheers,

diff --git a/lib/Expressions.php b/lib/Expressions.php
index ee7509c..0c1d076 100644
--- a/lib/Expressions.php
+++ b/lib/Expressions.php
@@ -132,6 +132,8 @@ class Expressions

            if (is_array($value))
                $sql .= "$g$name IN(?)";
+           else if (is_null($value))
+               $sql .= "$g$name IS ?";
            else
                $sql .= "$g$name=?";

diff --git a/test/SQLBuilderTest.php b/test/SQLBuilderTest.php
index 29ea734..4ce5e7a 100644
--- a/test/SQLBuilderTest.php
+++ b/test/SQLBuilderTest.php
@@ -67,6 +67,13 @@ class SQLBuilderTest extends DatabaseTest
        $this->assert_equals(array(1,'Tito','Mexican'),$this->sql->get_where_values());
    }

+   public function test_where_with_hash_and_null()
+   {
+       $this->sql->where(array('id' => 1, 'name' => null));
+       $this->assert_sql_has("SELECT * FROM authors WHERE id=? AND name IS ?",(string)$this->sql);
+       $this->assert_equals(array(1, null),$this->sql->get_where_values());
+   }
+
    public function test_where_with_null()
    {
        $this->sql->where(null);
@greut
Copy link
Collaborator Author

greut commented Mar 19, 2011

the commit: greut@7ef3060

@jpfuentes2
Copy link
Owner

Thanks:

490f119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants