Skip to content

[BUG]:Model::save(), hasOne relation, cause insertion of related #16000

Open
@zordon13

Description

@zordon13

Describe the bug
I have table test in database (PostgreSQL) and have view test_data of this table. When i save to table test from model - everything is fine.
When i getRealted model (from test_data view) and after save test model - orm try insert realted model (into view!)

To Reproduce
Steps to reproduce the behavior:

CREATE TABLE public.test
(
    id serial,
    str text COLLATE pg_catalog."default",
    CONSTRAINT test_pkey PRIMARY KEY (id)
)

CREATE OR REPLACE VIEW public.test_data
 AS
 SELECT test.id,
    test.str,
    now() AS dtn
   FROM test;

class TestData extends \Phalcon\Mvc\Model{    

    public $id;
    public $str;
    public $dtn;  

    public function initialize() {
        $this->setSchema("public");
        $this->setSource('test_data');        
    }
}

class Test extends \Phalcon\Mvc\Model{    
    public $id;
    public $str;   

    public function initialize() {
        $this->setSchema("public");
        $this->setSource('test');
        $this->hasOne('id', TestData::class, 'id', ['alias' => 'TestData']);
    }
    public function getTestData(): TestData {
        return $this->getRelated('TestData');
    } 
}

Normal behavior 1:

$test = new  Test;
$test->str = 'Hello';
$test->save();

Normal behavior 2:

$test = Test::findFirst();
$test->str = 'Hello World';
$test->save();

Unexpected behavior

$test = Test::findFirst();
var_dump($test->toArray());
//get related "view"
$testd = $test->getTestData();
var_dump($testd->toArray());        
//modify Test (not TestData)
$test->str = 'Good Bye World!';        
$test->save();

Output

array(2) {
  ["id"]=>
  int(1)
  ["str"]=>
  string(11) "Hello World"
}
array(3) {
  ["id"]=>
  int(1)
  ["str"]=>
  string(11) "Hello World"
  ["dtn"]=>
  string(29) "2022-06-25 14:36:08.890285+03"
}
SQLSTATE[0A000]: Feature not supported: 7 ERROR:  cannot insert into column "dtn" of view "test_data"
DETAIL:  View columns that are not columns of their base relation are not updatable.

**SQL log **
from event manager

[2022-06-25T14:38:43+03:00][DEBUG] SELECT "test"."id", "test"."str" FROM "public"."test" LIMIT :APL0
[2022-06-25T14:38:43+03:00][DEBUG] array (
  'APL0' => 1,
)
[2022-06-25T14:38:43+03:00][DEBUG] SELECT "test_data"."id", "test_data"."str", "test_data"."dtn" FROM "public"."test_data" WHERE "test_data"."id" = :APR0 LIMIT :APL0
[2022-06-25T14:38:43+03:00][DEBUG] array (
  'APR0' => 1,
  'APL0' => 1,
)
[2022-06-25T14:38:43+03:00][DEBUG] UPDATE "public"."test" SET "str" = ? WHERE "id" = ?
[2022-06-25T14:38:43+03:00][DEBUG] array (
  0 => 'Good Bye World!',
  1 => 1,
)
[2022-06-25T14:38:43+03:00][DEBUG] INSERT INTO "public"."test_data" ("id", "str", "dtn") VALUES (?, ?, ?)
[2022-06-25T14:38:43+03:00][DEBUG] array (
  0 => 1,
  1 => 'Hello World',
  2 => '2022-06-25 14:38:43.161401+03',
)

Details

  • Phalcon version: (php --ri phalcon)
phalcon


Phalcon is a full stack PHP framework, delivered as a PHP extension, offering lower resource consumption and high performance.
phalcon => enabled
Author => Phalcon Team and contributors
Version => 5.0.0RC2
Build Date => Jun 24 2022 23:12:58
Powered by Zephir => Version 0.16.0-4fac47b

Directive => Local Value => Master Value
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.case_insensitive_column_map => Off => Off
phalcon.orm.cast_last_insert_id_to_int => Off => Off
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.column_renaming => On => On
phalcon.orm.disable_assign_setters => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.enable_literals => On => On
phalcon.orm.events => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.exception_on_failed_metadata_save => On => On
phalcon.orm.ignore_unknown_columns => Off => Off
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.not_null_validations => On => On
phalcon.orm.resultset_prefetch_records => 0 => 0
phalcon.orm.update_snapshot_on_save => On => On
phalcon.orm.virtual_foreign_keys => On => On
phalcon.warning.enable => On => On
  • PHP Version: (php -v)
PHP 8.1.7 (cli) (built: Jun 14 2022 10:26:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.7, Copyright (c) Zend Technologies
   with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies
  • Operating System:
    Operating System: Debian GNU/Linux 10 (buster)

  • Installation type: Compiling from source

  • Other related info (Database, table schema):
    (PostgreSQL) 14.4 (Debian 14.4-1.pgdg100+1)
    Additional context
    Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions