Hi all,
I have found very buggy behaviour in saving a model with relations. Let's suppose that we have two classes:
<?php
class Album extends \Phalcon\Mvc\Model
{
public $id;
public $name;
}
class Song extends \Phalcon\Mvc\Model
{
public $id;
public $albumId;
public $name;
public function initialize()
{
$this->hasOne('albumId', '\Album', 'id', [
'alias' => 'album'
]);
}
}
Each song can be related to some album. And we have following DB data:
|--------------|
| id | name |
|--------------|
| 1 | Album 1 |
| 2 | Album 2 |
|--------------|
|-----------------------|
| id | albumId | name |
|-----------------------|
| 1 | 1 | Song 1 |
|-----------------------|
Now let's move song ID#1 from Album 1 to Album 2
$song = \Song::findFirst(1);
$song->albumId = 2;
$song->save();
Let's check the database:
|--------------|
| id | name |
|--------------|
| 1 | Album 1 |
| 2 | Album 2 |
|--------------|
|-----------------------|
| id | albumId | name |
|-----------------------|
| 1 | 2 | Song 1 |
|-----------------------|
Looks fine till now. But let's change the code a little (don't forget to move song record back to album 1). Let's decide that we want to print song's album name before we change it.
$song = \Song::findFirst(1);
echo $song->album->name;
$song->albumId = 2;
$song->save();
Now let's check the database:
|--------------|
| id | name |
|--------------|
| 1 | Album 1 |
| 2 | Album 1 |
|--------------|
|-----------------------|
| id | albumId | name |
|-----------------------|
| 1 | 2 | Song 1 |
|-----------------------|
What??? Album 2 now called Album 1?
Actually that is very simple example. In that case album model have only one field besides id, but in real case all fileds for changed record are replaced by values of previous record. In my opinion that is very unexpected behaviour and it should not work that way.
Tested on: PHP Version 5.6.21; Phalcon Version 3.0.0;