Skip to content

[BUG]: Cannot store multiple changed relation values  #15554

Open
@krazzer

Description

@krazzer

The bug
When trying to store multiple relations, only the last set value is stored.

To Reproduce

use Phalcon\Db\Adapter\Pdo\Sqlite;
use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Di;
use Phalcon\Mvc\Model;

$di = new Di();
$db = new Sqlite(["dbname" => ":memory:"]);

$di->set('db', $db);
$di->set('modelsManager', new Model\Manager());
$di->set('modelsMetadata', new Model\MetaData\Memory());

Di::setDefault($di);

$db->createTable('person', '', [
    'columns' => [
        new Column('id', ['type' => Column::TYPE_INTEGER, 'size' => 11, 'notNull' => true]),
        new Column('name', ['type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true]),
    ],
    'indexes' => [new Index('PRIMARY', ['id'])],
]);

$db->createTable('person_hair', '', [
    'columns' => [
        new Column('person_id', ['type' => Column::TYPE_INTEGER, 'size' => 11, 'notNull' => true]),
        new Column('color', ['type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true]),
        new Column('length', ['type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true]),
    ],
    'indexes' => [new Index('PRIMARY', ['person_id'])],
]);

class Person extends Model
{
    public function initialize()
    {
        $this->setSource('person');
        $this->hasOne('id', PersonHair::class, 'person_id', ['alias' => 'hair']);
    }
}

class PersonHair extends Model
{
    public function initialize()
    {
        $this->setSource('person_hair');
    }
}

$db->insert('person', [1, 'Tim Cook'], ['id', 'name']);
$db->insert('person_hair', [1, 'Black', 'Long'], ['person_id', 'color', 'length']);

$timCook = Person::findFirst(1);

echo $timCook->name . ' : ' . $timCook->hair->color . ' : ' . $timCook->hair->length . PHP_EOL;

$timCook->hair->color  = 'Gray';
$timCook->hair->length = 'Short';
$timCook->save();

$timCook2 = Person::findFirst(1);

echo $timCook2->name . ' : ' . $timCook2->hair->color . ' : ' . $timCook2->hair->length . PHP_EOL;

Output

Tim Cook : Black : Long
Tim Cook : Black : Short

Expected output

Tim Cook : Black : Long
Tim Cook : Gray : Short

Details

  • Phalcon version: 4.1.0
  • PHP Version: 7.2.34
  • Operating System: MacOS
  • Installation type: Compiling from source / Cloudlinux
  • Server: Apache

Additional context
This problem occurs in 4.1.0 but not in 3.4.5

Metadata

Metadata

Assignees

Labels

5.0The issues we want to solve in the 5.0 releasebugA bug reportstatus: unverifiedUnverified

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions