From e46c5ba4afe59a0671c132c73db397ddaf7d6489 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Thu, 11 Aug 2016 23:06:52 +0300 Subject: [PATCH] Ability to use an empty array for Criteria::inWhere --- CHANGELOG.md | 1 + phalcon/mvc/model/criteria.zep | 35 ++++++++------ tests/unit/Mvc/Model/CriteriaTest.php | 70 +++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 tests/unit/Mvc/Model/CriteriaTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 41569e71a80..d07fdc4a13f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed virtual foreign key check when having multiple keys [#12071](https://github.com/phalcon/cphalcon/issues/12071) - `Phalcon\Config\Adapter\Ini` constructor can now specify `parse_ini_file()` scanner mode [#12079](https://github.com/phalcon/cphalcon/pull/12079) - Fixed `Phalcon\Cache\Backend\Apc::save` due to which the `Apc::increment`/`Apc::decrement` could not be used properly [#12109](https://github.com/phalcon/cphalcon/issues/12109) +- Fixed `Phalcon\Mvc\Model\Criteria::inWhere` so that now the second parameter can be an empty array [#10676](https://github.com/phalcon/cphalcon/issues/10676) # [3.0.0](https://github.com/phalcon/cphalcon/releases/tag/v3.0.0) (2016-07-29) - PHP 5.3 and 5.4 are now fully deprecated diff --git a/phalcon/mvc/model/criteria.zep b/phalcon/mvc/model/criteria.zep index f7e523192ac..fb4ecaa1175 100644 --- a/phalcon/mvc/model/criteria.zep +++ b/phalcon/mvc/model/criteria.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -33,15 +33,15 @@ use Phalcon\Mvc\Model\ResultsetInterface; * Phalcon\Mvc\Model::find() and Phalcon\Mvc\Model::findFirst() * using an object-oriented interface. * - * - *$robots = Robots::query() - * ->where("type = :type:") - * ->andWhere("year < 2000") - * ->bind(array("type" => "mechanical")) - * ->limit(5, 10) - * ->orderBy("name") - * ->execute(); - * + * + * $robots = Robots::query() + * ->where('type = :type:') + * ->andWhere('year < 2000') + * ->bind(['type' => 'mechanical']) + * ->limit(5, 10) + * ->orderBy('name') + * ->execute(); + * */ class Criteria implements CriteriaInterface, InjectionAwareInterface { @@ -432,19 +432,22 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface /** * Appends an IN condition to the current conditions * - * - * $criteria->inWhere('id', [1, 2, 3]); - * + * + * $criteria->inWhere('id', [1, 2, 3]); + * */ public function inWhere(string! expr, array! values) -> { var hiddenParam, bindParams, bindKeys, value, key, queryKey; - let hiddenParam = this->_hiddenParamNumber; + if !count(values) { + this->andWhere(expr . " != " . expr); + return this; + } - let bindParams = []; - let bindKeys = []; + let hiddenParam = this->_hiddenParamNumber; + let bindParams = [], bindKeys = []; for value in values { /** diff --git a/tests/unit/Mvc/Model/CriteriaTest.php b/tests/unit/Mvc/Model/CriteriaTest.php new file mode 100644 index 00000000000..a9852add7af --- /dev/null +++ b/tests/unit/Mvc/Model/CriteriaTest.php @@ -0,0 +1,70 @@ + + * @author Serghei Iakovlev + * @package Phalcon\Test\Unit\Mvc\Model + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class CriteriaTest extends UnitTest +{ + /** + * executed before each test + */ + protected function _before() + { + parent::_before(); + + $di = $this->tester->getApplication()->getDI(); + + $di->set('modelsManager', function() { + return new Manager; + }); + + $di->set('modelsMetadata', function() { + return new Memory; + }); + + Di::setDefault($di); + } + + /** + * Tests Criteria::inWhere with empty array. + * + * @issue 10676 + * @author Serghei Iakovlev + * @since 2016-08-11 + */ + public function testShouldExecuteInWhereQueryWithEmptyArray() + { + $this->specify( + 'The Criteria::inWhere with empty array does not work as expected', + function() { + $criteria = Users::query()->inWhere(Users::class . '.id', []); + + expect($criteria->getWhere())->equals(Users::class . '.id != ' . Users::class . '.id'); + expect($criteria->execute())->isInstanceOf(Simple::class); + } + ); + } +}