-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[BUG]: Model virtual FK query not saving new value after execution #15649
Comments
/cc @niden |
I can suggest a complete test case along with DB tables. Everything is very "basic": I use
CREATE TABLE `zzz_test_a` (
`zzz_test_a_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`description` varchar(255) DEFAULT NULL,
PRIMARY KEY (`zzz_test_a_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `zzz_test_b` (
`zzz_test_b_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`zzz_test_a_id` int(10) unsigned NOT NULL,
`table_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`zzz_test_b_id`),
KEY `zzz_test_b_zzz_test_a_idx` (`zzz_test_a_id`),
CONSTRAINT `zzz_test_b_zzz_test_a` FOREIGN KEY (`zzz_test_a_id`) REFERENCES `zzz_test_a` (`zzz_test_a_id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
class ZzzTestA extends \Phalcon\Mvc\Model {
// Protected properties for fields with public getters and setters
public function initialize() {
$this->setConnectionService( 'db' );
$this->setSchema( 'db_name' );
$this->useDynamicUpdate( true );
$this->keepSnapshots( true );
$this->setSource( 'zzz_test_a' );
$this->hasMany( 'Id', ZzzTestB::class, 'ZzzTestAId', [ 'alias' => 'ZzzTestBs' ] );
}
public function columnMap() {
return [
'zzz_test_a_id' => 'Id',
'description' => 'Description',
];
}
}
class ZzzTestB extends \Phalcon\Mvc\Model {
// Protected properties for fields with public getters and setters
public function initialize() {
$this->setConnectionService( 'db' );
$this->setSchema( 'db_name' );
$this->useDynamicUpdate( true );
$this->keepSnapshots( true );
$this->setSource( 'zzz_test_a' );
$this->belongsTo( 'ZzzTestAId', ZzzTestA::class, 'Id', [ 'alias' => 'ZzzTestA' ] );
}
public function columnMap() {
return [
'zzz_test_b_id' => 'Id',
'zzz_test_a_id' => 'ZzzTestAId',
'table_name' => 'TableName',
];
}
} Test case$zzzA = new ZzzTestA();
$zzzA->setDescription( 'Test A 100' );
$zzzA->save();
$id1 = $zzzA->getId();
$zzzA = new ZzzTestA();
$zzzA->setDescription( 'Test A 200' );
$zzzA->save();
$id2 = $zzzA->getId();
$this->assertNotEquals( $id1, $id2 );
$zzzB = new ZzzTestB();
$zzzB->setZzzTestAId( $id1 );
$zzzB->setTableName( 'Test B 100' );
$zzzB->save();
$idB = $zzzB->getId();
$testB = ZzzTestB::findFirst( $idB );
$this->assertEquals( $id1, (int)$testB->getZzzTestAId(), 'FIRST CHECK' );
$testB->getZzzTestA();
$testB->setZzzTestAId( $id2 );
$result = $testB->save();
$this->assertTrue( $result );
$this->assertEquals( $id2, (int)$testB->getZzzTestAId(), 'SECOND CHECK' ); Test case result:
|
I tried to work on " In my previous example (#15649 (comment)), inside public function save(): bool {
$this->related['ZzzTestA'] = ZzzTestA::findFirstById( $this->ZzzTestAId );
return parent::save();
} The test passes. Inside extended This "fixes" the problem. This is obviously not an adequate fix, but a clue as to what could be causing the problem. |
I think the possible bug is here:
I think this is not correct:
because During If I extend the method inside my public function getRelated( string $alias, $arguments = null ) {
if( is_null( $arguments ) ) {
$arguments = [];
}
return parent::getRelated( $alias, $arguments );
} The test passes. I force |
Sorry, in my previous comment I said "I think this is not correct:" regarding this line: |
same issue here |
Addressed in #15702 |
Describe the bug
Related to: #15625, in particular this comment: #15625 (comment)
When using a Model with a "
belongsTo()
" relationship it is possible to use the "navigation magic method" using thealias
name given tobelongsTo()
's options to obtain the related FK Model instance from the first Model's instance.If I use that magic method before changing the value of that FK on the first Model's instance, after saving the model the FK value is still the previous one.
To Reproduce
I will show an example using model "
Promotion
", which contains a FK to another model "Person
".Model
Promotion
:NOT working example:
Working example:
PHP version: 7.4.22
Phalcon version: 4.1.2
MySql 5.7 (Percona), with InnoDB tables - FKs are handled by MySql
Debian GNU/Linux 10
I am using dynamic updates and snapshots on all Models, but disabling them does not change anything.
I tried to explicitly specify
'reusable' => false
inbelongsTo()
's options but it didn't change anything.I tried to disable FOREIGN_KEY_CHECKS on MySql before exetuing the test code, but it didn't change anything.
I tried to setup models at the beginning with:
'virtualForeignKeys' => false,
, but it didn't change anything.I'm not using any cache inside my Models, at least AFAIK: I have not explicitly specified any cache settings in any of my models. I'm using Phalcon's Cache but not inside Models, just to cache some data to reuse it, but nothing related to Models.
Expected behavior
The new FK value is saved with or without using its magic FK method before changing it.
I am upgrading from Phalcon 3.4.4 to Phalcon 4.1.2.
Everything works correctly except for this behaviour.
The code didn't change during the upgrade and the exact same code is working on:
PHP version: 7.3.27
Phalcon version: 3.4.4
and it's NOT working on:
PHP version: 7.4.22
Phalcon version: 4.1.2
Details
php -v
)The text was updated successfully, but these errors were encountered: