Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #14674 from phalcon/fix/#14669-query-builder-colum…
Browse files Browse the repository at this point in the history
…n-map

#14669 - Fix alias columns in Query Builder Column map
  • Loading branch information
Jeckerson committed Jan 2, 2020
2 parents 6930b33 + 39b0390 commit 15f4092
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG-4.0.md
Expand Up @@ -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 `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)

Expand Down
5 changes: 4 additions & 1 deletion phalcon/Mvc/Model/Resultset/Simple.zep
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/_data/fixtures/Traits/DiTrait.php
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions tests/_data/fixtures/models/InvoicesWithColumnMap.php
@@ -0,0 +1,41 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* 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',
];
}
}
123 changes: 121 additions & 2 deletions tests/integration/Mvc/Model/Resultset/Simple/ToArrayCest.php
Expand Up @@ -14,21 +14,140 @@
namespace Phalcon\Test\Integration\Mvc\Model\Resultset\Simple;

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
*/
class ToArrayCest
{
use DiTrait;

/**
* Tests Phalcon\Mvc\Model\Resultset\Simple :: toArray()
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-01-01
*/
public function mvcModelResultsetSimpleToArray(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Resultset\Simple - toArray()');
$I->skipTest('Need implementation');

/**
* Init Di with services
*/
$this->newDi();
$this->setDiModelsManager();
$this->setDiModelsMetadata();
$this->setDiMysql();

/**
* 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([
'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('Inv2.id DESC')
->columns([
'Inv2.id',
'Inv2.status',
'invoice_title' => 'Inv2.title',
])
->getQuery()
->execute();

$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);
$untouchedArray2 = $rows2->toArray(false);
$untouchedArray3 = $rows3->toArray(false);
$untouchedArray4 = $rows4->toArray(false);
$untouchedArray5 = $rows5->toArray(false);
$untouchedArray6 = $rows6->toArray(false);

/**
* Assert
*/
$I->assertInternalType('array', $renamedArray1);
$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('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);
}
}

0 comments on commit 15f4092

Please sign in to comment.