Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T16320 model toarray getter #16469

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
*
* 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 <[email protected]>
* @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);
}
}
Loading