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

Fix #16343, DynamicUpdate is now enabled system wide #16400

Merged
merged 1 commit into from
Aug 12, 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
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 likely 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 <[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\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');
}
}
219 changes: 219 additions & 0 deletions tests/database/Mvc/Model/DynamicUpdateCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?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\Integration\Mvc\Model;

use DatabaseTester;
use Phalcon\Events\Event;
use Phalcon\Events\Manager;
use Phalcon\Support\Collection;
use Phalcon\Tests\Fixtures\Migrations\CustomersMigration;
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 <[email protected]>
* @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');

$connection = $I->getConnection();

$customersMigration = new CustomersMigration($connection);
$customersMigration->insert(90, 1, null, null);

/**
* 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
* @var Customer
*/
$customer = Customers::findFirst(['cst_id=:id:', 'bind' => ['id' => 90]]);
$customer->cst_name_first = 'enableDynamicUpdate';

$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 <[email protected]>
* @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
* @var Customer
*/
$customer = Customers::findFirst(['cst_id=:id:', 'bind' => ['id' => 90]]);
$customer->cst_name_first = 'disableDynamicUpdate';
$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 = 4;
$actual = count($collection->get('0'));
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Mvc\Model :: save() with DynamicUpdate Disabled Cherry pick
*
* @author Phalcon Team <[email protected]>
* @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
* @var CustomersDymanicUpdate
*/
$customer = CustomersDymanicUpdate::findFirst(['cst_id=:id:', 'bind' => ['id' => 90]]);

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

$collection->clear();

$customer->cst_name_first = 'disabledCherryPickDynamicUpdate';
$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',
],
];
}
}
Loading