From cd3c32796ca84c4c60a5b87b89656375a3b7b1fe Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 19:33:19 -0600 Subject: [PATCH 1/5] added check for getters in model:toArray --- phalcon/Mvc/Model.zep | 11 +++- .../_data/fixtures/models/InvoicesGetters.php | 30 +++++++++++ tests/database/Mvc/Model/ToArrayCest.php | 50 +++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/_data/fixtures/models/InvoicesGetters.php diff --git a/phalcon/Mvc/Model.zep b/phalcon/Mvc/Model.zep index 3fc3a8c6ab..b16403129a 100644 --- a/phalcon/Mvc/Model.zep +++ b/phalcon/Mvc/Model.zep @@ -3277,7 +3277,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface, */ public function toArray(columns = null) -> array { - var metaData, columnMap, attribute, attributeField, value; + var attribute, attributeField, columnMap, metaData, method, value; array data; let data = [], @@ -3316,7 +3316,14 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface, } } - if fetch value, this->{attributeField} { + /** + * Check if there is a getter for this property + */ + let method = "get" . camelize(attributeField); + + if method_exists(this, method) { + let data[attributeField] = this->{method}(); + } elseif fetch value, this->{attributeField} { let data[attributeField] = value; } else { let data[attributeField] = null; diff --git a/tests/_data/fixtures/models/InvoicesGetters.php b/tests/_data/fixtures/models/InvoicesGetters.php new file mode 100644 index 0000000000..7afed10bba --- /dev/null +++ b/tests/_data/fixtures/models/InvoicesGetters.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Fixtures\models; + +use Phalcon\Tests\Models\Invoices; + +/** + * Class InvoicesGetters + */ +class InvoicesGetters extends Invoices +{ + /** + * @return bool|null + */ + public function getInvTitle(): string + { + return $this->inv_title . '!' . $this->inv_id; + } +} diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index 3b9cdb5bff..38efd402eb 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -17,10 +17,17 @@ use PDO; use Phalcon\Mvc\Model\Manager; use Phalcon\Tests\Fixtures\Migrations\InvoicesMigration; +use Phalcon\Tests\Fixtures\Migrations\SettersMigration; +use Phalcon\Tests\Fixtures\Migrations\SourcesMigration; +use Phalcon\Tests\Fixtures\models\InvoicesGetters; +use Phalcon\Tests\Fixtures\models\SourcesGetters; use Phalcon\Tests\Fixtures\Traits\DiTrait; use Phalcon\Tests\Models\Invoices; use Phalcon\Tests\Models\InvoicesMap; +use Phalcon\Tests\Models\Sources; + +use function date; use function uniqid; class ToArrayCest @@ -322,4 +329,47 @@ public function mvcModelToArrayExecuteColumnNotInColumnMap(DatabaseTester $I) $actual = $result->toArray(); $I->assertSame($expected, $actual); } + + /** + * Tests Phalcon\Mvc\Model\ :: save() with property source + * + * @author Phalcon Team + * @since 2019-11-16 + * @issue #11922 + * + * @group mysql + * @group sqlite + */ + public function mvcModelToArrayModelWithGetters(DatabaseTester $I) + { + $I->wantToTest('Mvc\Model - toArray - model with getters'); + + /** @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); + + $model = InvoicesGetters::findFirst(4); + + $class = InvoicesGetters::class; + $I->assertInstanceOf($class, $model); + + $expected = 4; + $actual = $model->inv_id; + $I->assertEquals($expected, $actual); + + $expected = [ + 'inv_id' => 4, + 'inv_cst_id' => 1, + 'inv_status_flag' => 0, + 'inv_title' => $title . '!4', + 'inv_total' => 111.26, + 'inv_created_at' => $date, + ]; + $actual = $model->toArray(); + $I->assertSame($expected, $actual); + } } From 60d4fc3f16befaa0c2c4c812be3861a80580a353 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 19:34:33 -0600 Subject: [PATCH 2/5] updated changelog --- CHANGELOG-5.0.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index cf6acda1c4..965b76895f 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -4,6 +4,8 @@ ### Changed +- Changed `Phalcon\Mvc\Model::toArray` to use getters if present [#16320](https://github.com/phalcon/cphalcon/issues/16320) + ### Added ### Fixed From 2ea48f5bea95fcb79153012e697f6a75c9eb55e7 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 23 Nov 2023 19:38:06 -0600 Subject: [PATCH 3/5] phpcs --- tests/database/Mvc/Model/ToArrayCest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index 38efd402eb..fb5c806737 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -24,7 +24,6 @@ use Phalcon\Tests\Fixtures\Traits\DiTrait; use Phalcon\Tests\Models\Invoices; use Phalcon\Tests\Models\InvoicesMap; - use Phalcon\Tests\Models\Sources; use function date; From 9e0fc82fe5543bd5b777791e158afd017723eb3d Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Fri, 24 Nov 2023 04:46:09 -0600 Subject: [PATCH 4/5] correcting sqlite test --- tests/database/Mvc/Model/ToArrayCest.php | 31 ++++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index fb5c806737..e2e1099aaf 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -360,14 +360,29 @@ public function mvcModelToArrayModelWithGetters(DatabaseTester $I) $actual = $model->inv_id; $I->assertEquals($expected, $actual); - $expected = [ - 'inv_id' => 4, - 'inv_cst_id' => 1, - 'inv_status_flag' => 0, - 'inv_title' => $title . '!4', - 'inv_total' => 111.26, - 'inv_created_at' => $date, - ]; + /** + * sqlite returns strings + */ + if ('sqlite' === $I->getDriver()) { + $expected = [ + 'inv_id' => '4', + 'inv_cst_id' => '1', + 'inv_status_flag' => '0', + 'inv_title' => $title . '!4', + 'inv_total' => '111.26', + 'inv_created_at' => $date, + ]; + } else { + $expected = [ + 'inv_id' => 4, + 'inv_cst_id' => 1, + 'inv_status_flag' => 0, + 'inv_title' => $title . '!4', + 'inv_total' => 111.26, + 'inv_created_at' => $date, + ]; + }; + $actual = $model->toArray(); $I->assertSame($expected, $actual); } From 7618025f7fcb87ab7c81e41b22e61e45fa1a81e0 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Fri, 24 Nov 2023 05:02:23 -0600 Subject: [PATCH 5/5] correcting test for sqlite and php 8.+ --- tests/database/Mvc/Model/ToArrayCest.php | 36 +++++++++--------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/tests/database/Mvc/Model/ToArrayCest.php b/tests/database/Mvc/Model/ToArrayCest.php index e2e1099aaf..ab65eac398 100644 --- a/tests/database/Mvc/Model/ToArrayCest.php +++ b/tests/database/Mvc/Model/ToArrayCest.php @@ -360,30 +360,20 @@ public function mvcModelToArrayModelWithGetters(DatabaseTester $I) $actual = $model->inv_id; $I->assertEquals($expected, $actual); - /** - * sqlite returns strings - */ - if ('sqlite' === $I->getDriver()) { - $expected = [ - 'inv_id' => '4', - 'inv_cst_id' => '1', - 'inv_status_flag' => '0', - 'inv_title' => $title . '!4', - 'inv_total' => '111.26', - 'inv_created_at' => $date, - ]; - } else { - $expected = [ - 'inv_id' => 4, - 'inv_cst_id' => 1, - 'inv_status_flag' => 0, - 'inv_title' => $title . '!4', - 'inv_total' => 111.26, - 'inv_created_at' => $date, - ]; - }; + $expected = [ + 'inv_id' => '4', + 'inv_cst_id' => '1', + 'inv_status_flag' => '0', + 'inv_title' => $title . '!4', + 'inv_total' => '111.26', + 'inv_created_at' => $date, + ]; $actual = $model->toArray(); - $I->assertSame($expected, $actual); + /** + * assertEquals here because sqlite returns strings in different + * PHP versions + */ + $I->assertEquals($expected, $actual); } }