Skip to content

Commit

Permalink
T13336 find first null (#13826)
Browse files Browse the repository at this point in the history
* [4.0.x] - Added unicode flag for email filter

* [#13336] - Adjustments to handling parameters for findFirst with primary key

* [#13336] - Updated the changelog
  • Loading branch information
niden authored Feb 16, 2019
1 parent 6da12e7 commit c310058
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fixed `invalid opcode` in `phalcon.so` when using docker [#13143](https://github.com/phalcon/cphalcon/issues/13143)
- Fixed storing related model data in `Phalcon\Messages\Message`. The method is now `setMetadata` and can be used to store any metadata from any component that emits messages [#13811](https://github.com/phalcon/cphalcon/issues/13811)
- Fixed Dispatcher calling camelize twice and producing incorrect results [#12829](https://github.com/phalcon/cphalcon/issues/12829)
- Fixed `Phalcon\Mvc\Model:findFirst` to throw an exception when the passed parameter for a primary key is not an array, string or numeric [#13336](https://github.com/phalcon/cphalcon/issues/13336)

## Changed
- Renamed `Phalcon\Acl\Subject` to `Phalcon\Acl\Component` [#13808](https://github.com/phalcon/cphalcon/issues/13808)
Expand Down
12 changes: 7 additions & 5 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1268,13 +1268,15 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
{
var params, query;

if typeof parameters != "array" {
if null === parameters {
let params = [];
if parameters !== null {
let params[] = parameters;
}
} else {
} elseif typeof parameters === "array" {
let params = parameters;
} elseif (typeof parameters === "string" || true === is_numeric(parameters)) {
let params = [];
let params[] = parameters;
} else {
throw new Exception("Parameters passed must be of type array, string, numeric or null");
}

let query = static::getPreparedQuery(params, 1);
Expand Down
43 changes: 42 additions & 1 deletion tests/integration/Mvc/Model/FindFirstCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
namespace Phalcon\Test\Integration\Mvc\Model;

use IntegrationTester;
use Phalcon\Mvc\Model\Exception;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Models\Robots;

/**
* Class FindFirstCest
*/
class FindFirstCest
{
use DiTrait;

/**
* Tests Phalcon\Mvc\Model :: findFirst()
*
Expand All @@ -30,6 +35,42 @@ class FindFirstCest
public function mvcModelFindFirst(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model - findFirst()');
$I->skipTest('Need implementation');
$this->setNewFactoryDefault();
$this->setDiMysql();

$robot = Robots::findFirst();
$class = Robots::class;
$I->assertInstanceOf($class, $robot);
$I->assertEquals(1, $robot->id);

$robot = Robots::findFirst(null);
$class = Robots::class;
$I->assertInstanceOf($class, $robot);

$robot = Robots::findFirst(1);
$class = Robots::class;
$I->assertInstanceOf($class, $robot);
}

/**
* Tests Phalcon\Mvc\Model :: findFirst() - exception
*
* @param IntegrationTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function mvcModelFindFirstException(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model - findFirst() - exception');
$this->setNewFactoryDefault();
$this->setDiMysql();

$I->expectThrowable(
new Exception("Parameters passed must be of type array, string, numeric or null"),
function () {
Robots::findFirst(false);
}
);
}
}

0 comments on commit c310058

Please sign in to comment.