Skip to content

Commit 7212ba0

Browse files
authored
Merge pull request #16491 from rudiservo/i16490-model-toarray-serialize
Model::toArray added parameter to ignore getters
2 parents 043a91a + 48d7102 commit 7212ba0

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

CHANGELOG-5.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## [5.5.0](https://github.com/phalcon/cphalcon/releases/tag/v5.5.1) (xxxx-xx-xx)
3+
## [5.5.1](https://github.com/phalcon/cphalcon/releases/tag/v5.5.1) (xxxx-xx-xx)
44

55
### Changed
66

@@ -12,6 +12,7 @@
1212
### Fixed
1313

1414
- Fixed `Phalcon\Mvc\Model::count` to ignore the `order` parameter (needed for Posgresql) [#16471](https://github.com/phalcon/cphalcon/issues/16471)
15+
- Fixed `Phalcon\Mvc\Model::toArray` added parameter to ignore getters in order not to break serialize. [#16490](https://github.com/phalcon/cphalcon/issues/16490)
1516

1617
### Removed
1718

phalcon/Mvc/Model.zep

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
390390
*/
391391
var attributes, manager, dirtyState, snapshot = null;
392392

393-
let attributes = this->toArray(),
393+
let attributes = this->toArray(null, false),
394394
dirtyState = this->dirtyState,
395395
manager = <ManagerInterface> this->getModelsManager();
396396

@@ -2791,7 +2791,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
27912791
*/
27922792
var attributes, manager, dirtyState, snapshot = null;
27932793

2794-
let attributes = this->toArray(),
2794+
let attributes = this->toArray(null, false),
27952795
dirtyState = this->dirtyState,
27962796
manager = <ManagerInterface> this->getModelsManager();
27972797

@@ -3282,7 +3282,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
32823282
*
32833283
* @param array $columns
32843284
*/
3285-
public function toArray(columns = null) -> array
3285+
public function toArray(columns = null, useGetter = true) -> array
32863286
{
32873287
var attribute, attributeField, columnMap, metaData, method, value;
32883288
array data;
@@ -3328,7 +3328,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
33283328
*/
33293329
let method = "get" . camelize(attributeField);
33303330

3331-
if method_exists(this, method) {
3331+
if true === useGetter && method_exists(this, method) {
33323332
let data[attributeField] = this->{method}();
33333333
} elseif fetch value, this->{attributeField} {
33343334
let data[attributeField] = value;

tests/database/Mvc/Model/ToArrayCest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,58 @@ public function mvcModelToArrayModelWithGetters(DatabaseTester $I)
407407
* PHP versions
408408
*/
409409
$I->assertEquals($expected, $actual);
410+
411+
$expected = [
412+
'inv_id' => '4',
413+
'inv_cst_id' => '1',
414+
'inv_status_flag' => '0',
415+
'inv_title' => $title,
416+
'inv_total' => '111.26',
417+
'inv_created_at' => $date,
418+
];
419+
$actual = $model->toArray(null, false);
420+
$I->assertEquals($expected, $actual);
421+
}
422+
423+
/**
424+
* Tests Phalcon\Mvc\Model\ :: save() with property source
425+
*
426+
* @author Phalcon Team <team@phalcon.io>
427+
* @since 2019-11-16
428+
* @issue #11922
429+
*
430+
* @group mysql
431+
* @group sqlite
432+
*/
433+
public function mvcModelToArrayModelWithGettersSerialize(DatabaseTester $I)
434+
{
435+
$I->wantToTest('Mvc\Model - toArray - model with getters serialize');
436+
437+
/** @var PDO $connection */
438+
$connection = $I->getConnection();
439+
$title = uniqid('inv-');
440+
$date = date('Y-m-d H:i:s');
441+
442+
$migration = new InvoicesMigration($connection);
443+
$migration->insert(4, 1, 0, $title, 111.26, $date);
444+
445+
$model = InvoicesGetters::findFirst(4);
446+
447+
$class = InvoicesGetters::class;
448+
$I->assertInstanceOf($class, $model);
449+
450+
$expected = 4;
451+
$actual = $model->inv_id;
452+
$I->assertEquals($expected, $actual);
453+
454+
/**
455+
* assertEquals here because sqlite returns strings in different
456+
* PHP versions
457+
*/
458+
459+
$serialize = $model->serialize();
460+
$unserialize = new InvoicesGetters();
461+
$unserialize->unserialize($serialize);
462+
$I->assertEquals($title, $unserialize->inv_title);
410463
}
411464
}

0 commit comments

Comments
 (0)