Skip to content

Commit

Permalink
Merge pull request phalcon#15764 from niden/T15361-find-toarray
Browse files Browse the repository at this point in the history
T15361 find toarray
  • Loading branch information
niden authored Nov 4, 2021
2 parents b61c6d2 + 995f52a commit e07f05f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
- Fixed `Phalcon\Storage\Adapter\Redis::getAdapter()` and `Phalcon\Cache\Adapter\Redis::getAdapter()` to accept the connection timeout in the constructor `options` [#15744](https://github.com/phalcon/cphalcon/issues/15744)
- Fixed `Phalcon\Db\Adapter\AbstractAdapter::getSQLVariables()` to return an empty array when initialized [#15637](https://github.com/phalcon/cphalcon/issues/15637)
- Fixed `Phalcon\Cache\Adapter\*` and `Phalcon\Storage\Adapter\*` to delete a key when `set()` is called with a zero or negative TTL [#15485](https://github.com/phalcon/cphalcon/issues/15485)
- Fixed `Phalcon\Db\Adapter\Pdo\Mysql` to not use `PDO::ATTR_EMULATE_PREPARES` and `PDO::ATTR_STRINGIFY_FETCHES` by default. This allows numbers to be returned with resultsets instead of strings for numeric fields [#15361](https://github.com/phalcon/cphalcon/issues/15361)

# [5.0.0alpha6](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0alpha6) (2021-09-16)

Expand Down
32 changes: 32 additions & 0 deletions phalcon/Db/Adapter/Pdo/Mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ class Mysql extends PdoAdapter
*/
protected type = "mysql";

/**
* Constructor for Phalcon\Db\Adapter\Pdo
*
* @param array descriptor = [
* 'host' => 'localhost',
* 'port' => '3306',
* 'dbname' => 'blog',
* 'username' => 'sigma'
* 'password' => 'secret'
* 'dialectClass' => null,
* 'options' => [],
* 'dsn' => null,
* 'charset' => 'utf8mb4'
* ]
*/
public function __construct(array! descriptor)
{
/**
* Returning numbers as numbers and not strings. If the user already
* set this option in the descriptor["options"], we do not have to set
* anything
*/
if (!isset(descriptor["options"][\PDO::ATTR_EMULATE_PREPARES])) {
let descriptor["options"][\PDO::ATTR_EMULATE_PREPARES] = false;
}
if (!isset(descriptor["options"][\PDO::ATTR_STRINGIFY_FETCHES])) {
let descriptor["options"][\PDO::ATTR_STRINGIFY_FETCHES] = false;
}

parent::__construct(descriptor);
}

/**
* Adds a foreign key to a table
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/database/Db/Adapter/Pdo/ConnectCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Phalcon\Tests\Database\Db\Adapter\Pdo;

use DatabaseTester;
use PDO;
use Phalcon\Db\Adapter\PdoFactory;
use Phalcon\Tests\Fixtures\Traits\DiTrait;

Expand Down Expand Up @@ -51,6 +52,10 @@ public function dbAdapterPdoConnectPersistent(DatabaseTester $I)

$options = getOptionsMysql();
$options['persistent'] = true;
$options['options'] = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_STRINGIFY_FETCHES => false,
];

$connection = (new PdoFactory())->newInstance('mysql', $options);

Expand Down
103 changes: 94 additions & 9 deletions tests/database/Mvc/Model/ToArrayCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ public function _before(DatabaseTester $I)
/**
* Tests Phalcon\Mvc\Model :: toArray()
*
* @group mysql
* @group pgsql
* @group sqlite
* @author Phalcon Team <team@phalcon.io>
* @since 2021-11-03
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelToArray(DatabaseTester $I)
{
Expand Down Expand Up @@ -87,9 +90,12 @@ public function mvcModelToArray(DatabaseTester $I)
/**
* Tests Phalcon\Mvc\Model :: toArray() - column map
*
* @group mysql
* @group pgsql
* @group sqlite
* @author Phalcon Team <team@phalcon.io>
* @since 2021-11-03
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelToArrayColumnMap(DatabaseTester $I)
{
Expand Down Expand Up @@ -135,10 +141,13 @@ public function mvcModelToArrayColumnMap(DatabaseTester $I)
/**
* Tests Phalcon\Mvc\Model :: toArray() - find first columns
*
* @issue 1701
* @author Phalcon Team <team@phalcon.io>
* @since 2021-11-03
*
* @issue https://github.com/phalcon/cphalcon/issues/1701
*
* @group mysql
* @group sqlite
* @group mysql
* @group sqlite
*/
public function mvcModelToArrayFindFirstColumns(DatabaseTester $I)
{
Expand Down Expand Up @@ -176,4 +185,80 @@ public function mvcModelToArrayFindFirstColumns(DatabaseTester $I)
$actual = $invoice->toArray();
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Mvc\Model :: toArray() - find - castOnHydrate/forceCasting
*
* @author Phalcon Team <team@phalcon.io>
* @since 2021-11-03
*
* @issue https://github.com/phalcon/cphalcon/issues/15361
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelToArrayFindCastOnHydrateForceCasting(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - toArray() - find - castOnHydrate/forceCasting');

/** @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);
$migration->insert(5, 2, 1, $title, 222.19, $date);

Invoices::setup(
[
'forceCasting' => true,
'castOnHydrate' => true,
]
);

$invoices = Invoices::findFirst();

$expected = [
'inv_id' => 4,
'inv_cst_id' => 1,
'inv_status_flag' => 0,
'inv_title' => $title,
'inv_total' => 111.26,
'inv_created_at' => $date,
];
$actual = $invoices->toArray();
$I->assertEquals($expected, $actual);

$invoices = Invoices::find();

$expected = [
[
'inv_id' => 4,
'inv_cst_id' => 1,
'inv_status_flag' => 0,
'inv_title' => $title,
'inv_total' => 111.26,
'inv_created_at' => $date,
],
[
'inv_id' => 5,
'inv_cst_id' => 2,
'inv_status_flag' => 1,
'inv_title' => $title,
'inv_total' => 222.19,
'inv_created_at' => $date,
]
];
$actual = $invoices->toArray();
$I->assertSame($expected, $actual);

Invoices::setup(
[
'forceCasting' => false,
'castOnHydrate' => false,
]
);
}
}

0 comments on commit e07f05f

Please sign in to comment.