From e221e9fb6103754090e1788617853380f1d9c70e Mon Sep 17 00:00:00 2001 From: DarkAngelBGE Date: Tue, 10 Jun 2008 16:17:44 +0000 Subject: [PATCH] forcing the db layer to make an WHERE field IS NULL when find statements are called with empty arrays, closes #4845 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7155 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo/dbo_mysql.php | 2 +- cake/libs/model/datasources/dbo_source.php | 4 ++-- .../cases/libs/model/datasources/dbo_source.test.php | 8 ++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 8a9b548a1..aa33572a8 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -214,7 +214,7 @@ class DboMysql extends DboSource { if ($parent != null) { return $parent; - } elseif ($data === null) { + } elseif ($data === null || (is_array($data) && empty($data))) { return 'NULL'; } elseif ($data === '') { return "''"; diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index e823d3262..0efcd06b8 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -132,7 +132,7 @@ class DboSource extends DataSource { * @return mixed Prepared value or array of values. */ function value($data, $column = null) { - if (is_array($data)) { + if (is_array($data) && !empty($data)) { return array_map(array(&$this, 'value'), $data, array_fill(0, count($data), $column)); } elseif (is_object($data)) { if (isset($data->type) && $data->type == 'identifier') { @@ -1833,7 +1833,7 @@ class DboSource extends DataSource { } if ( - (is_array($value) && is_string($value[0]) && strpos($value[0], '-!') === 0) || + (is_array($value) && !empty($value) && is_string($value[0]) && strpos($value[0], '-!') === 0) || (is_string($value) && strpos($value, '-!') === 0) ) { trigger_error(__('Escape flag (-!) deprecated, use Model::query()', true), E_USER_WARNING); diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index b2767a7c6..b46905dd5 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -2350,6 +2350,14 @@ class DboSourceTest extends CakeTestCase { $expected = " WHERE `score` IN (1, 2, 10)"; $this->assertEqual($result, $expected); + $result = $this->testDb->conditions(array('score' => array())); + $expected = " WHERE `score` IS NULL"; + $this->assertEqual($result, $expected); + + $result = $this->testDb->conditions(array('score !=' => array())); + $expected = " WHERE `score` IS NOT NULL"; + $this->assertEqual($result, $expected); + $result = $this->testDb->conditions(array('score !=' => '20')); $expected = " WHERE `score` != '20'"; $this->assertEqual($result, $expected);