From 14192b382207c3ddaf2c5b414995576e4acfd95d Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 12:06:44 -0600 Subject: [PATCH 1/4] added conditional to ignore numeric indexes in case results come not fetch_assoc --- phalcon/Mvc/Model/Resultset/Simple.zep | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/phalcon/Mvc/Model/Resultset/Simple.zep b/phalcon/Mvc/Model/Resultset/Simple.zep index 320ab145da5..11c11862618 100644 --- a/phalcon/Mvc/Model/Resultset/Simple.zep +++ b/phalcon/Mvc/Model/Resultset/Simple.zep @@ -212,24 +212,26 @@ class Simple extends Resultset let renamed = []; for key, value in record { - /** - * Check if the key is part of the column map - */ - if unlikely !fetch renamedKey, columnMap[key] { - throw new Exception( - "Column '" . key . "' is not part of the column map" - ); - } - - if typeof renamedKey == "array" { - if unlikely !fetch renamedKey, renamedKey[0] { + if (typeof key === "string") { + /** + * Check if the key is part of the column map + */ + if unlikely !fetch renamedKey, columnMap[key] { throw new Exception( "Column '" . key . "' is not part of the column map" ); } - } - let renamed[renamedKey] = value; + if typeof renamedKey == "array" { + if unlikely !fetch renamedKey, renamedKey[0] { + throw new Exception( + "Column '" . key . "' is not part of the column map" + ); + } + } + + let renamed[renamedKey] = value; + } } /** From 08445435ea54d2f8df86698be635136995fcee64 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 12:06:51 -0600 Subject: [PATCH 2/4] Added test --- tests/database/Mvc/Model/ToArrayCest.php | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index 11cddf4698a..c535aa158f3 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -15,12 +15,14 @@ use DatabaseTester; use PDO; +use Phalcon\Mvc\Model\Manager; use Phalcon\Tests\Fixtures\Migrations\InvoicesMigration; use Phalcon\Tests\Fixtures\Traits\DiTrait; use Phalcon\Tests\Models\Invoices; use Phalcon\Tests\Models\InvoicesMap; use function uniqid; +use function var_dump; class ToArrayCest { @@ -259,4 +261,65 @@ public function mvcModelToArrayFindCastOnHydrateForceCasting(DatabaseTester $I) ] ); } + + /** + * Tests Phalcon\Mvc\Model :: toArray() - execute column not in columnMap + * + * @author Phalcon Team + * @since 2022-11-21 + * + * @issue https://github.com/phalcon/cphalcon/issues/16467 + * + * @group mysql + */ + public function mvcModelToArrayExecuteColumnNotInColumnMap(DatabaseTester $I) + { + $I->wantToTest('Mvc\Model - toArray() - execute - column not in columnMap'); + + /** @var PDO $connection */ + $connection = $I->getConnection(); + $title = uniqid('inv-'); + $date = date('Y-m-d H:i:s'); + + $migration = new InvoicesMigration($connection); + $migration->insert(4, 1, 0, $title, 111.26, $date); + $migration->insert(5, 2, 1, $title, 222.19, $date); + + $manager = $this->getService('modelsManager'); + $class = Manager::class; + $I->assertInstanceOf($class, $manager); + + + $result = $manager + ->createBuilder() + ->addFrom(InvoicesMap::class, 'i') + ->limit(10) + ->getQuery() + ->execute(); + + $result->rewind(); + $result->next(); + $result->rewind(); + + $expected = [ + [ + 'id' => 4, + 'cst_id' => 1, + 'status_flag' => 0, + 'title' => $title, + 'total' => 111.26, + 'created_at' => $date, + ], + [ + 'id' => 5, + 'cst_id' => 2, + 'status_flag' => 1, + 'title' => $title, + 'total' => 222.19, + 'created_at' => $date, + ], + ]; + $actual = $result->toArray(); + $I->assertSame($expected, $actual); + } } From cca222a09893bae6094c02276e930409b4e7532c Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 12:08:15 -0600 Subject: [PATCH 3/4] updated changelog --- CHANGELOG-5.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index caabaaa962a..cf6acda1c4a 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -9,6 +9,7 @@ ### Fixed - Fixed `Phalcon\Filter\Validation\Validator\Numericality` to return false when input has spaces [#16461](https://github.com/phalcon/cphalcon/issues/16461) +- Fixed `Phalcon\Mvc\Model\ResultsetSimple::toArray` to ignore numeric indexes in case results come as not `fetch_assoc` [#16467](https://github.com/phalcon/cphalcon/issues/16467) ### Removed From db0108d493f1d852d3e34100fb7b8f5021837bbf Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 12:14:17 -0600 Subject: [PATCH 4/4] removed unecessary import --- tests/database/Mvc/Model/ToArrayCest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index c535aa158f3..3b9cdb5bffa 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -22,7 +22,6 @@ use Phalcon\Tests\Models\InvoicesMap; use function uniqid; -use function var_dump; class ToArrayCest { @@ -249,7 +248,7 @@ public function mvcModelToArrayFindCastOnHydrateForceCasting(DatabaseTester $I) 'inv_title' => $title, 'inv_total' => 222.19, 'inv_created_at' => $date, - ] + ], ]; $actual = $invoices->toArray(); $I->assertSame($expected, $actual); @@ -295,7 +294,8 @@ public function mvcModelToArrayExecuteColumnNotInColumnMap(DatabaseTester $I) ->addFrom(InvoicesMap::class, 'i') ->limit(10) ->getQuery() - ->execute(); + ->execute() + ; $result->rewind(); $result->next(); @@ -319,7 +319,7 @@ public function mvcModelToArrayExecuteColumnNotInColumnMap(DatabaseTester $I) 'created_at' => $date, ], ]; - $actual = $result->toArray(); + $actual = $result->toArray(); $I->assertSame($expected, $actual); } }