Skip to content
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

updates doesn't work on views? #85

Open
kumy opened this issue Aug 10, 2019 · 1 comment
Open

updates doesn't work on views? #85

kumy opened this issue Aug 10, 2019 · 1 comment

Comments

@kumy
Copy link
Contributor

kumy commented Aug 10, 2019

I'm working on rewriting an old application. I also wish to rename some tables and columns.

So I've created some views including the necessary changes and found that all table entries are updated.

Is this a bug or do I expect something that "can not work"?
Thanks


Here is a small example to demonstrate:

<?php // app/Member.php
class Member extends DB\Cortex {
	protected
		$db='DB',
		$table='member',
		$fieldConf=[
			'label' => ['type'=>\DB\SQL\Schema::DT_VARCHAR128],
			'flag' => ['type'=>\DB\SQL\Schema::DT_VARCHAR128],
		];
}
<?php  // index.php

require 'vendor/autoload.php';
$f3 = \Base::instance();
$f3->set('DB', new \DB\SQL('mysql:host=db;port=3306;dbname=db;charset=utf8mb4', 'user', ''));
$f3->set('AUTOLOAD', 'app/');

$f3->route('HEAD /', function () {});
$f3->route('GET /', function($f3) {
    Member::setup();
    echo "HELLO /";
});

$f3->route('GET /insert', function($f3) {
    $m = new Member();
    $m->label = 'John';
    $m->flag = 1;
    $m->save();
    $m2 = new Member();
    $m2->label = 'Doe';
    $m2->flag = 0;
    $m2->save();
});

$f3->route('GET /update', function($f3) {
    $m = new Member();
    $m->load(array('label = ?', 'Doe'));
    $m->flag = abs($m->flag -1);
    $m->save();
    echo $f3->get('DB')->log();
});
$f3->run();

step 1

Step to reproduce

  • navigate to / -> /insert -> /update [refresh /update]
  • Watch table content

Actual result
Only the row with id 1 should be updated

Expected result
Only the row with id 1 should be updated

👍 Normal fine!

step 2

Step to reproduce

  • Now create a view matching exactly the table structure.
CREATE VIEW `member2` AS
SELECT id, label, flag
FROM `member`;
  • update Member class to declare the view: $table='member2',
  • navigate to / -> /insert -> /update [refresh /update]

Actual result
All the rows are updated

Expected result
Only the row with id 1 should be updated

Notes
The sql dump show that in step2, the sql update ommit the where clause, which is why all rows are updated.

(0.8ms) SHOW columns FROM `geokrety`.`member2`
(0.3ms) SELECT `id`,`label`,`flag` FROM `member2` WHERE `label` = 'Doe'
(4.6ms) UPDATE `member2` SET `flag`='0'

Whereas in step1 the where condition exists

(0.6ms) SHOW columns FROM `geokrety`.`member`
(0.2ms) SELECT `id`,`label`,`flag` FROM `member` WHERE `label` = 'Doe'
(4.7ms) UPDATE `member` SET `flag`='1' WHERE `id`=1
@ikkez
Copy link
Owner

ikkez commented Aug 12, 2019

It's an "issue" in the F3 sql mapper, but it's a feature that I think cannot be implemented reliably, because the mapper/sql engine isnt able to get the primary key from a view schema. That's why you're not able to update a single record.. the information schema table doesn seem to give the correct data here, or the schema query need to be updated for it.. I'm not sure atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants