Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T16467 column 0 to array #16468

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading