Skip to content

Commit

Permalink
Merge pull request #16468 from niden/T16467-column-0-to-array
Browse files Browse the repository at this point in the history
T16467 column 0 to array
  • Loading branch information
niden authored Nov 23, 2023
2 parents 88ea794 + db0108d commit 20cf90f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 15 additions & 13 deletions phalcon/Mvc/Model/Resultset/Simple.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down
65 changes: 64 additions & 1 deletion tests/database/Mvc/Model/ToArrayCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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;
Expand Down Expand Up @@ -247,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);
Expand All @@ -259,4 +260,66 @@ public function mvcModelToArrayFindCastOnHydrateForceCasting(DatabaseTester $I)
]
);
}

/**
* Tests Phalcon\Mvc\Model :: toArray() - execute column not in columnMap
*
* @author Phalcon Team <[email protected]>
* @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);
}
}

0 comments on commit 20cf90f

Please sign in to comment.