Skip to content

Commit

Permalink
Fix phalcon#16343, DynamicUpdate is now enabled system wide
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiservo committed Aug 11, 2023
1 parent f3f6cc8 commit c24e555
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Fixed

- Parse multipart/form-data from PUT request [#16271](https://github.com/phalcon/cphalcon/issues/16271)
- Set Dynamic Update by default system wide [#16343](https://github.com/phalcon/cphalcon/issues/16343)


## [5.2.3](https://github.com/phalcon/cphalcon/releases/tag/v5.2.3) (2023-07-26)
Expand Down
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@
"type": "bool",
"default": true
},
"orm.dynamic_update": {
"type": "bool",
"default": true
},
"warning.enable": {
"type": "bool",
"default": true
Expand Down
2 changes: 1 addition & 1 deletion phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
let columnMap = null;
}

if useDynamicUpdate && typeof snapshot !== "array" {
if useDynamicUpdate && typeof snapshot === "array" {
for field in nonPrimary {
let changed = false;
if typeof columnMap === "array" {
Expand Down
8 changes: 8 additions & 0 deletions phalcon/Mvc/Model/Manager.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,10 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI
{
var isKeeping;

if globals_get("orm.dynamic_update") {
return true;
}

if !fetch isKeeping, this->keepSnapshots[get_class_lower(model)] {
return false;
}
Expand All @@ -1866,6 +1870,10 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI
{
var isUsing;

if globals_get("orm.dynamic_update") {
return true;
}

if !fetch isUsing, this->dynamicUpdate[get_class_lower(model)] {
return false;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/_data/fixtures/models/CustomersDymanicUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Models;

use Phalcon\Mvc\Model;

/**
* Class CustomersKeepSnapshots
*
* @property int $cst_id;
* @property int $cst_status_flag;
* @property string $cst_name_last;
* @property string $cst_name_first;
*/
class CustomersDymanicUpdate extends Model
{
public $cst_id;
public $cst_status_flag;
public $cst_name_last;
public $cst_name_first;

public function initialize()
{
$this->useDynamicUpdate(true);
$this->setSource('co_customers');
}
}
216 changes: 216 additions & 0 deletions tests/database/Mvc/Model/DynamicUpdateCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?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\Integration\Mvc\Model;

use DatabaseTester;
use Phalcon\Events\Event;
use Phalcon\Events\Manager;
use Phalcon\Support\Collection;
use Phalcon\Tests\Fixtures\Traits\DiTrait;
use Phalcon\Tests\Models\Customers;
use Phalcon\Tests\Models\CustomersDymanicUpdate;

class DynamicUpdateCest
{
use DiTrait;

public function _before(DatabaseTester $I)
{
$this->setNewFactoryDefault();
$this->setDatabase($I);
}

public function _after(DatabaseTester $I)
{
$this->container['db']->close();
}

/**
* Tests Phalcon\Mvc\Model :: save() With DynamicUpdate Enabled
*
* @author Phalcon Team <team@phalcon.io>
* @since 2023-08-11
*
* @issue https://github.com/phalcon/cphalcon/issues/16343
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function enableDynamicUpdate(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - DynamicUpdate System Wide Enabled');

/**
* Check system wide Dynamic update
*/
$actual = ini_get('phalcon.orm.dynamic_update');
$I->assertEquals("1", $actual);


$collection = new Collection();
$connection = $this->container->get('db');
$manager = new Manager();
$modelsManager = $this->container->get('modelsManager');
$manager->attach('db:beforeQuery', function (Event $event) use ($connection, $collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLVariables());
});

$connection->setEventsManager($manager);

/**
* New model
*/
$customer = new Customers();
$customer->cst_name_first = 'cst_test_firstName';

$actual = $customer->save();
$I->assertTrue($actual);

$actual = $modelsManager->isUsingDynamicUpdate($customer);
$I->assertTrue($actual);

$collection->clear();

$customer->cst_name_last = 'cst_test_lastName';

$actual = $customer->save();
$I->assertTrue($actual);

$expected = 2;
$actual = count($collection->get('0'));
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Mvc\Model :: save() with DynamicUpdate Disabled
*
* @author Phalcon Team <team@phalcon.io>
* @since 2023-08-11
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function disableDynamicUpdate(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - DynamicUpdate Systeam Wide Disabled');
$collection = new Collection();

$connection = $this->container->get('db');
$manager = new Manager();
$modelsManager = $this->container->get('modelsManager');
$manager->attach('db:beforeQuery', function (Event $event) use ($connection, $collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLVariables());
});

$connection->setEventsManager($manager);

/**
* Disable system wide dynamic update
*/
ini_set('phalcon.orm.dynamic_update', "0");

/**
* Check system wide Dynamic update
*/
$actual = ini_get('phalcon.orm.dynamic_update');

$I->assertEquals("0", $actual);

/**
* New model
*/
$customer = new Customers();
$customer->cst_name_first = 'cst_test_firstName';

$actual = $customer->save();
$I->assertTrue($actual);

$actual = $modelsManager->isUsingDynamicUpdate($customer);
$I->assertFalse($actual);

$collection->clear();

$customer->cst_name_last = 'cst_test_lastName';

$actual = $customer->save();
$I->assertTrue($actual);

$expected = 3;
$actual = count($collection->get('0'));
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Mvc\Model :: save() with DynamicUpdate Disabled Cherry pick
*
* @author Phalcon Team <team@phalcon.io>
* @since 2023-08-11
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function disabledCherryPickDynamicUpdate(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - DynamicUpdate Systeam Wide Disabled Cherry pick');

$collection = new Collection();
$connection = $this->container->get('db');
$manager = new Manager();
$modelsManager = $this->container->get('modelsManager');
$manager->attach('db:beforeQuery', function (Event $event) use ($connection, $collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLVariables());
});

$connection->setEventsManager($manager);

/**
* Disable system wide dynamic update
*/
ini_set('phalcon.orm.dynamic_update', "0");

/**
* Check system wide Dynamic update
*/
$actual = ini_get('phalcon.orm.dynamic_update');
$I->assertEquals("0", $actual);

/**
* New model
*/
$customer = new CustomersDymanicUpdate();
$customer->cst_name_first = 'cst_test_firstName';

$actual = $customer->save();
$I->assertTrue($actual);

$actual = $modelsManager->isUsingDynamicUpdate($customer);
$I->assertTrue($actual);

$collection->clear();

$customer->cst_name_last = 'cst_test_lastName';
$actual = $customer->save();
$I->assertTrue($actual);

$expected = 2;
$actual = count($collection->get('0'));
$I->assertEquals($expected, $actual);
}
}
4 changes: 4 additions & 0 deletions tests/unit/Mvc/GlobalsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ private function getExamples(): array
'setting' => 'phalcon.orm.virtual_foreign_keys',
'value' => '1',
],
[
'setting' => 'phalcon.orm.dynamic_update',
'value' => '1',
],
];
}
}

0 comments on commit c24e555

Please sign in to comment.