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;
Tim Cook : Black : Long
Tim Cook : Black : Short
Tim Cook : Black : Long
Tim Cook : Gray : Short
The bug
When trying to store multiple relations, only the last set value is stored.
To Reproduce
Output
Expected output
Details
Additional context
This problem occurs in 4.1.0 but not in 3.4.5