Skip to content

Commit

Permalink
Fix #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 8c0db10
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 0 deletions.
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
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');
}
}
218 changes: 218 additions & 0 deletions tests/database/Mvc/Model/DynamicUpdateCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?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 DynamicUpdateEnabled(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - DynamicUpdate Systeam 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, $connection) use ($collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLStatement() . json_encode($connection->getSQLVariables()));
}
);

$connection->setEventsManager($manager);

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

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

$customer->cst_name_last = 'cst_test_lastName';

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

$expected = 'UPDATE `co_customers` SET `cst_name_last` = ? \
WHERE `cst_id` = ?["cst_test_lastName","' . $customer->cst_id . '"]';
$actual = $collection->get('0');
$I->assertSame($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 DynamicUpdateDisabled(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, $connection) use ($collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLStatement() . json_encode($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();

$I->assertFalse($modelsManager->isUsingDynamicUpdate($customer));
$customer->cst_name_first = 'cst_test_firstName';

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

$customer->cst_name_last = 'cst_test_lastName';

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

$expected = 'UPDATE `co_customers` \
SET `cst_status_flag` = null, `cst_name_last` = ?, `cst_name_first` = ? \
WHERE `cst_id` = ?["cst_test_lastName","cst_test_firstName","' . $customer->cst_id . '"]';
$actual = $collection->get('0');
$I->assertSame($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 DynamicUpdateDisabledCherryPick(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, $connection) use ($collection) {
$key = (string) $collection->count();
$collection->set($key, $connection->getSQLStatement() . json_encode($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();
$I->assertTrue($modelsManager->isUsingDynamicUpdate($customer));
$customer->cst_name_first = 'cst_test_firstName';
$actual = $customer->save();
$I->assertTrue($actual);
$collection->clear();

$customer->cst_name_last = 'cst_test_lastName';

$actual = $customer->save();

$expected = 'UPDATE `co_customers` SET `cst_name_last` = ? \
WHERE `cst_id` = ?["cst_test_lastName","' . $customer->cst_id . '"]';
$actual = $collection->get('0');
$I->assertSame($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 8c0db10

Please sign in to comment.