From ddbdf170e6c7cb3fe574a485cd8ba8c67a5c3f9e Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Thu, 8 Jun 2017 20:22:29 +0200 Subject: [PATCH 1/2] Fix discrepancy in Model::field when Model::id is null When using ClassRegistry::init for instance --- lib/Cake/Model/Model.php | 2 +- lib/Cake/Test/Case/Model/ModelReadTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 1f2b1effb..e41eefbb1 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1653,7 +1653,7 @@ class Model extends CakeObject implements CakeEventListener { * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-field */ public function field($name, $conditions = null, $order = null) { - if ($conditions === null && $this->id !== false) { + if ($conditions === null && !in_array($this->id, [false, null], true)) { $conditions = array($this->alias . '.' . $this->primaryKey => $this->id); } diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 48ca338a5..876ac57f2 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -7041,6 +7041,12 @@ class ModelReadTest extends BaseModelTest { $result = $TestModel->field('COUNT(*)', true); $this->assertEquals(4, $result); + + $TestModel->id = null; + $result = $TestModel->field('user', array( + 'user' => 'mariano' + )); + $this->assertEquals('mariano', $result); } /** From 377aa2aa74855748bb34694003a2edfdf1601a38 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Thu, 8 Jun 2017 20:31:34 +0200 Subject: [PATCH 2/2] Drop short array syntax for PHP < 5.4 --- lib/Cake/Model/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index e41eefbb1..2acc2cafe 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1653,7 +1653,7 @@ class Model extends CakeObject implements CakeEventListener { * @link http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-field */ public function field($name, $conditions = null, $order = null) { - if ($conditions === null && !in_array($this->id, [false, null], true)) { + if ($conditions === null && !in_array($this->id, array(false, null), true)) { $conditions = array($this->alias . '.' . $this->primaryKey => $this->id); }