Skip to content

Commit

Permalink
Merge pull request #16469 from niden/T16320-model-toarray-getter
Browse files Browse the repository at this point in the history
T16320 model toarray getter
  • Loading branch information
niden authored Nov 24, 2023
2 parents 20cf90f + 7618025 commit 3b561e3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/_data/fixtures/models/InvoicesGetters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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.
*/

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;
}
}
54 changes: 54 additions & 0 deletions tests/database/Mvc/Model/ToArrayCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
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
Expand Down Expand Up @@ -322,4 +328,52 @@ public function mvcModelToArrayExecuteColumnNotInColumnMap(DatabaseTester $I)
$actual = $result->toArray();
$I->assertSame($expected, $actual);
}

/**
* Tests Phalcon\Mvc\Model\ :: save() with property source
*
* @author Phalcon Team <team@phalcon.io>
* @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();
/**
* assertEquals here because sqlite returns strings in different
* PHP versions
*/
$I->assertEquals($expected, $actual);
}
}

0 comments on commit 3b561e3

Please sign in to comment.