From 10c560e3d2c52aac38657b46bc7bb78528937611 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Wed, 1 Jan 2020 23:50:52 +0000 Subject: [PATCH 1/8] #14669 - Fix import of namespaces --- tests/_data/fixtures/Traits/DiTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/_data/fixtures/Traits/DiTrait.php b/tests/_data/fixtures/Traits/DiTrait.php index 1055cd6d925..1c945769012 100644 --- a/tests/_data/fixtures/Traits/DiTrait.php +++ b/tests/_data/fixtures/Traits/DiTrait.php @@ -36,8 +36,8 @@ use Phalcon\Filter; use Phalcon\Http\Request; use Phalcon\Http\Response; -use Phalcon\Mvc\Models\Manager as ModelsManager; -use Phalcon\Mvc\Models\Metadata\Memory as MetadataMemory; +use Phalcon\Mvc\Model\Manager as ModelsManager; +use Phalcon\Mvc\Model\Metadata\Memory as MetadataMemory; use Phalcon\Mvc\View; use Phalcon\Mvc\View\Simple; use Phalcon\Session\Adapter\Libmemcached as SessionLibmemcached; From 1a74160eb769aadd0d7b960865ade24ca79d5cc8 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Wed, 1 Jan 2020 23:54:04 +0000 Subject: [PATCH 2/8] #14669 - Implement tests for ToArrayCest --- .../Model/Resultset/Simple/ToArrayCest.php | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php index 0b27ee18538..2e148b82c71 100644 --- a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php +++ b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php @@ -13,22 +13,51 @@ namespace Phalcon\Test\Integration\Mvc\Model\Resultset\Simple; +use Phalcon\Mvc\Model\Query\Builder; use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\DiTrait; +use Phalcon\Test\Models\Robots; /** * Class ToArrayCest */ class ToArrayCest { + use DiTrait; + /** * Tests Phalcon\Mvc\Model\Resultset\Simple :: toArray() * * @author Phalcon Team - * @since 2018-11-13 + * @since 2020-01-01 */ public function mvcModelResultsetSimpleToArray(IntegrationTester $I) { $I->wantToTest('Mvc\Model\Resultset\Simple - toArray()'); - $I->skipTest('Need implementation'); + + $this->newDi(); + $this->setDiModelsManager(); + $this->setDiModelsMetadata(); + $this->setDiMysql(); + + $rows = (new Builder()) + ->from([ + 'Robots' => Robots::class, + ]) + ->orderBy('Robots.id DESC') + ->columns([ + 'Robots.id', + 'Robots.type', + 'robot_name' => 'Robots.name', + ]) + ->getQuery() + ->execute(); + + //$renamedArray = $rows->toArray(); + $untouchedArray = $rows->toArray(false); + + $I->assertInternalType('array', $untouchedArray); + //$I->assertInternalType('array', $renamedArray); + $I->assertArrayHasKey('robot_name', $untouchedArray[0]); } } From 9cb1a0795ee36e2d62ceacc761b4b4ed098aea7f Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 01:09:16 +0000 Subject: [PATCH 3/8] #14669 - Change condition of columnMap renaming --- phalcon/Mvc/Model/Resultset/Simple.zep | 5 ++++- tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/phalcon/Mvc/Model/Resultset/Simple.zep b/phalcon/Mvc/Model/Resultset/Simple.zep index 2f006089085..5b7ef048e37 100644 --- a/phalcon/Mvc/Model/Resultset/Simple.zep +++ b/phalcon/Mvc/Model/Resultset/Simple.zep @@ -16,6 +16,7 @@ use Phalcon\Di\DiInterface; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Exception; use Phalcon\Mvc\Model\Resultset; +use Phalcon\Mvc\Model\Row; use Phalcon\Mvc\ModelInterface; use Phalcon\Storage\Serializer\SerializerInterface; @@ -181,8 +182,10 @@ class Simple extends Resultset /** * We need to rename the whole set here, this could be slow + * + * Only rename when it is Model */ - if renameColumns { + if renameColumns && !(this->model instanceof Row) { /** * Get the resultset column map */ diff --git a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php index 2e148b82c71..a2c909313dc 100644 --- a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php +++ b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php @@ -53,11 +53,12 @@ public function mvcModelResultsetSimpleToArray(IntegrationTester $I) ->getQuery() ->execute(); - //$renamedArray = $rows->toArray(); + $renamedArray = $rows->toArray(); $untouchedArray = $rows->toArray(false); $I->assertInternalType('array', $untouchedArray); - //$I->assertInternalType('array', $renamedArray); + $I->assertInternalType('array', $renamedArray); $I->assertArrayHasKey('robot_name', $untouchedArray[0]); + $I->assertArrayHasKey('robot_name', $renamedArray[0]); } } From 127ed62f738de87109b00e269561275b688c57b7 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 12:09:50 +0000 Subject: [PATCH 4/8] #14669 - Rewrite ToArrayCest tests --- .../Model/Resultset/Simple/ToArrayCest.php | 63 +++++++++++++++---- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php index a2c909313dc..40224d6e6b9 100644 --- a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php +++ b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php @@ -15,8 +15,9 @@ use Phalcon\Mvc\Model\Query\Builder; use IntegrationTester; +use Phalcon\Test\Fixtures\Migrations\InvoicesMigration; use Phalcon\Test\Fixtures\Traits\DiTrait; -use Phalcon\Test\Models\Robots; +use Phalcon\Test\Models\Invoices; /** * Class ToArrayCest @@ -35,30 +36,66 @@ public function mvcModelResultsetSimpleToArray(IntegrationTester $I) { $I->wantToTest('Mvc\Model\Resultset\Simple - toArray()'); + /** + * Init Di with services + */ $this->newDi(); $this->setDiModelsManager(); $this->setDiModelsMetadata(); $this->setDiMysql(); - $rows = (new Builder()) + /** + * Re-create DB table with data + */ + $db = $this->getService('db'); + (new InvoicesMigration())($db); + $data = [ + [1, 0, 'Title 1', 10.51, date('Y-m-d H:i:s')], + [123, 1, 'Title 2', 5.2, date('Y-m-d H:i:s')], + [321, 1, 'Title 3', 0.25, null], + ]; + + foreach ($data as $row) { + $db->insert( + 'co_invoices', + $row, + ['inv_cst_id', 'inv_status_flag', 'inv_title', 'inv_total', 'inv_created_at'] + ); + } + + /** + * Prepare data to assert + */ + $rows1 = (new Builder()) ->from([ - 'Robots' => Robots::class, + 'Invoices' => Invoices::class, ]) - ->orderBy('Robots.id DESC') + ->orderBy('Invoices.inv_id DESC') ->columns([ - 'Robots.id', - 'Robots.type', - 'robot_name' => 'Robots.name', + 'Invoices.inv_cst_id', + 'Invoices.inv_status_flag', + 'invoice_title' => 'Invoices.inv_title', ]) ->getQuery() ->execute(); + $rows2 = Invoices::find(); + + $renamedArray1 = $rows1->toArray(); + $untouchedArray1 = $rows1->toArray(false); + $renamedArray2 = $rows2->toArray(); + $untouchedArray2 = $rows2->toArray(false); - $renamedArray = $rows->toArray(); - $untouchedArray = $rows->toArray(false); + /** + * Assert + */ + $I->assertInternalType('array', $untouchedArray1); + $I->assertInternalType('array', $renamedArray1); + $I->assertInternalType('array', $untouchedArray2); + $I->assertInternalType('array', $renamedArray2); - $I->assertInternalType('array', $untouchedArray); - $I->assertInternalType('array', $renamedArray); - $I->assertArrayHasKey('robot_name', $untouchedArray[0]); - $I->assertArrayHasKey('robot_name', $renamedArray[0]); + $I->assertArrayHasKey('invoice_title', $untouchedArray1[0]); + $I->assertArrayHasKey('invoice_title', $renamedArray1[0]); + $I->assertArrayHasKey('inv_title', $untouchedArray2[0]); + $I->assertArrayHasKey('inv_title', $renamedArray2[0]); } } From 0931eca8235c6b83e33ef01387bf8a35f46e67e0 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 13:45:30 +0000 Subject: [PATCH 5/8] #14669 - Create InvoicesWithColumnMap Test Model --- .../fixtures/models/InvoicesWithColumnMap.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/_data/fixtures/models/InvoicesWithColumnMap.php diff --git a/tests/_data/fixtures/models/InvoicesWithColumnMap.php b/tests/_data/fixtures/models/InvoicesWithColumnMap.php new file mode 100644 index 00000000000..93228ce1aab --- /dev/null +++ b/tests/_data/fixtures/models/InvoicesWithColumnMap.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Models; + +use Phalcon\Mvc\Model; + +class InvoicesWithColumnMap extends Model +{ + public $inv_id; + public $inv_cst_id; + public $inv_status_flag; + public $inv_title; + public $inv_total; + public $inv_created_at; + + public function initialize() + { + $this->setSource('co_invoices'); + } + + public function columnMap() + { + return [ + 'inv_id' => 'id', + 'inv_cst_id' => 'customerId', + 'inv_status_flag' => 'status', + 'inv_title' => 'title', + 'inv_total' => 'total', + 'inv_created_at' => 'createdAt', + ]; + } +} From eab1b34fe56f3af34002e3e4b6a97427a10d6e4f Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 13:46:16 +0000 Subject: [PATCH 6/8] #14669 - Add test cases in ToArrayCest --- .../Model/Resultset/Simple/ToArrayCest.php | 78 +++++++++++++++---- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php index 40224d6e6b9..b166492aecf 100644 --- a/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php +++ b/tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php @@ -13,11 +13,12 @@ namespace Phalcon\Test\Integration\Mvc\Model\Resultset\Simple; -use Phalcon\Mvc\Model\Query\Builder; use IntegrationTester; +use Phalcon\Mvc\Model\Query\Builder; use Phalcon\Test\Fixtures\Migrations\InvoicesMigration; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Models\Invoices; +use Phalcon\Test\Models\InvoicesWithColumnMap; /** * Class ToArrayCest @@ -68,34 +69,85 @@ public function mvcModelResultsetSimpleToArray(IntegrationTester $I) */ $rows1 = (new Builder()) ->from([ - 'Invoices' => Invoices::class, + 'Inv1' => Invoices::class, + ]) + ->orderBy('Inv1.inv_id DESC') + ->columns([ + 'Inv1.inv_cst_id', + 'Inv1.inv_status_flag', + 'invoice_title' => 'Inv1.inv_title', + ]) + ->getQuery() + ->execute(); + + $rows2 = (new Builder()) + ->from([ + 'Inv2' => InvoicesWithColumnMap::class, ]) - ->orderBy('Invoices.inv_id DESC') + ->orderBy('Inv2.id DESC') ->columns([ - 'Invoices.inv_cst_id', - 'Invoices.inv_status_flag', - 'invoice_title' => 'Invoices.inv_title', + 'Inv2.id', + 'Inv2.status', + 'invoice_title' => 'Inv2.title', ]) ->getQuery() ->execute(); - $rows2 = Invoices::find(); - $renamedArray1 = $rows1->toArray(); + $rows3 = Invoices::find(); + $rows4 = Invoices::findFirst(); + $rows5 = InvoicesWithColumnMap::find(); + $rows6 = InvoicesWithColumnMap::findFirst(); + + $renamedArray1 = $rows1->toArray(); + $renamedArray2 = $rows2->toArray(); + $renamedArray3 = $rows3->toArray(); + $renamedArray4 = $rows4->toArray(); + $renamedArray5 = $rows5->toArray(); + $renamedArray6 = $rows6->toArray(); $untouchedArray1 = $rows1->toArray(false); - $renamedArray2 = $rows2->toArray(); $untouchedArray2 = $rows2->toArray(false); + $untouchedArray3 = $rows3->toArray(false); + $untouchedArray4 = $rows4->toArray(false); + $untouchedArray5 = $rows5->toArray(false); + $untouchedArray6 = $rows6->toArray(false); /** * Assert */ - $I->assertInternalType('array', $untouchedArray1); $I->assertInternalType('array', $renamedArray1); - $I->assertInternalType('array', $untouchedArray2); $I->assertInternalType('array', $renamedArray2); + $I->assertInternalType('array', $renamedArray3); + $I->assertInternalType('array', $renamedArray4); + $I->assertInternalType('array', $renamedArray5); + $I->assertInternalType('array', $renamedArray6); + + $I->assertInternalType('array', $untouchedArray1); + $I->assertInternalType('array', $untouchedArray2); + $I->assertInternalType('array', $untouchedArray3); + $I->assertInternalType('array', $untouchedArray4); + $I->assertInternalType('array', $untouchedArray5); + $I->assertInternalType('array', $untouchedArray6); $I->assertArrayHasKey('invoice_title', $untouchedArray1[0]); $I->assertArrayHasKey('invoice_title', $renamedArray1[0]); - $I->assertArrayHasKey('inv_title', $untouchedArray2[0]); - $I->assertArrayHasKey('inv_title', $renamedArray2[0]); + + $I->assertArrayHasKey('invoice_title', $untouchedArray2[0]); + $I->assertArrayHasKey('invoice_title', $renamedArray2[0]); + + $I->assertArrayHasKey('inv_title', $untouchedArray3[0]); + $I->assertArrayHasKey('inv_title', $renamedArray3[0]); + + $I->assertArrayHasKey('inv_title', $untouchedArray4); + $I->assertArrayHasKey('inv_title', $renamedArray4); + + $I->assertArrayHasKey('inv_title', $untouchedArray5[0]); + $I->assertArrayHasKey('title', $renamedArray5[0]); + $I->assertArrayNotHasKey('title', $untouchedArray5[0]); + $I->assertArrayNotHasKey('inv_title', $renamedArray5[0]); + + $I->assertArrayHasKey('title', $untouchedArray6); + $I->assertArrayHasKey('title', $renamedArray6); + $I->assertArrayNotHasKey('inv_title', $untouchedArray6); + $I->assertArrayNotHasKey('inv_title', $renamedArray6); } } From f3e235be6fb840028c8cf01eef399836546002e0 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 14:05:10 +0000 Subject: [PATCH 7/8] #14669 - Update CHANGELOG-4.0.md --- CHANGELOG-4.0.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index 31e27085307..d63654239fd 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -9,7 +9,8 @@ - Fixed `Phalcon\Db\Dialect\Mysql` removing unnecessary parentheses for `double` and `float` [#14645](https://github.com/phalcon/cphalcon/pull/14645) [@pfz](https://github.com/pfz) - Fixed `Phalcon\Http\Cookie::delete` to parse the correct parameters - cannot use alternative syntax until PHP 7.3 [#14643](https://github.com/phalcon/cphalcon/issues/14643) - Fixed `Phalcon\Mvc\Model::__isset` to take into account non visible properties by checking the getter if it exists [#13518](https://github.com/phalcon/cphalcon/issues/13518) [#13900](https://github.com/phalcon/cphalcon/issues/13900) -- Fixed `Phalcon\Mvc\Model::__set` to return a more informative message if we are tying to access a non visible property [#13518](https://github.com/phalcon/cphalcon/issues/13518) [#13900](https://github.com/phalcon/cphalcon/issues/13900) +- Fixed `Phalcon\Mvc\Model::__set` to return a more informative message if we are tying to access a non visible property [#13518](https://github.com/phalcon/cphalcon/issues/13518) [#13900](https://github.com/phalcon/cphalcon/issues/13900) +- Fixed `toArray()` for `Phalcon\Mvc\Model\Query\Builder` [#14669](https://github.com/phalcon/cphalcon/issues/14669) # [4.0.0](https://github.com/phalcon/cphalcon/releases/tag/v4.0.0) (2019-12-21) From 39b039044c96a68524e997b2f6943ecf4f7e2227 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 2 Jan 2020 15:59:58 +0000 Subject: [PATCH 8/8] #14669 - Update CHANGELOG-4.0.md --- CHANGELOG-4.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index d63654239fd..ac0591f3cf4 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -10,7 +10,7 @@ - Fixed `Phalcon\Http\Cookie::delete` to parse the correct parameters - cannot use alternative syntax until PHP 7.3 [#14643](https://github.com/phalcon/cphalcon/issues/14643) - Fixed `Phalcon\Mvc\Model::__isset` to take into account non visible properties by checking the getter if it exists [#13518](https://github.com/phalcon/cphalcon/issues/13518) [#13900](https://github.com/phalcon/cphalcon/issues/13900) - Fixed `Phalcon\Mvc\Model::__set` to return a more informative message if we are tying to access a non visible property [#13518](https://github.com/phalcon/cphalcon/issues/13518) [#13900](https://github.com/phalcon/cphalcon/issues/13900) -- Fixed `toArray()` for `Phalcon\Mvc\Model\Query\Builder` [#14669](https://github.com/phalcon/cphalcon/issues/14669) +- Fixed `Phalcon\Mvc\Model\Resultset\Simple::toArray` to correctly process virtual fields [#14669](https://github.com/phalcon/cphalcon/issues/14669) # [4.0.0](https://github.com/phalcon/cphalcon/releases/tag/v4.0.0) (2019-12-21)