Skip to content

Fix table name source with Join#909

Closed
samsouthardjr wants to merge 5 commits intoatk4:developfrom
samsouthardjr:develop
Closed

Fix table name source with Join#909
samsouthardjr wants to merge 5 commits intoatk4:developfrom
samsouthardjr:develop

Conversation

@samsouthardjr
Copy link
Copy Markdown
Contributor

@samsouthardjr samsouthardjr commented Oct 22, 2021

No description provided.

… generating [where] or [having] portions of queries that referenced the alias instead of the de-aliased table.
@mvorisek mvorisek added the bug label Oct 22, 2021
@mvorisek
Copy link
Copy Markdown
Member

Code to reproduce:

<?php

use \Atk4\Data\Model;

require 'vendor/autoload.php';

class Par extends Model
{
    public $table = 'parent';
    
    protected function init(): void 
    {
        parent::init();
        $this->addField('name');
        $this->hasMany('children', ['model' => [Child::class]]);
    }
}

class Child extends Model
{
    public $table = 'child';
    
    protected function init(): void
    {
        parent::init();
        $j = $this->join('jointable');
        $j->hasOne('parent_id', ['model' => [Par::class]]);
    }
}


$a = new App();

$p = new Par($a->db);
$e = $p->createEntity();
$e->save(['name' => 'foo']);
$e2 = $e->ref('children')->createEntity();
$e2->save();
$e->ref('children')->action('delete')->execute();

@samsouthardjr
Copy link
Copy Markdown
Contributor Author

samsouthardjr commented Oct 22, 2021

Code to reproduce:

Due to the join, the execute example is bad -- it will still produce an error. A better example is:

foreach ($e->ref('children') as $c) {
    $c->delete();
}


protected function _render_table_noalias(): ?string
{
$this->deAliasMainTable = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this relies on a fact that the _render_table_noalias is called first which is very bad assumption as it must not be always true like when WITH clause is used

IMHO we should check why some clauses use non-aliased table name in general

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a better version be to do something like changing a template to:
'[noalias][with]update [table_noalias] set [set] [where]' (and perhaps if [noalias] was at the beginning, [table_noalias] would be redundant)? With that syntax, we could just have a _render_noalias function that did nothing but set deAliasMaintable to true.

@samsouthardjr
Copy link
Copy Markdown
Contributor Author

samsouthardjr commented Oct 22, 2021 via email

@mvorisek mvorisek changed the title Fixed bug where using a template with [table_noalias] would result in… Fix table name source with Join Oct 22, 2021
@mvorisek
Copy link
Copy Markdown
Member

mvorisek commented Nov 10, 2021

How is the code supposed to work when there is no jointable defined?

here is a testable demo https://github.com/atk4/data/compare/fix_upd_join_w_alias#diff-92f78239a1e95b6ddebce64dec3b75d71cfa81ee4073bb544f9f8f491a37218cR736

please make it passing, eg. add extra 1:1 model /w jointable

@samsouthardjr
Copy link
Copy Markdown
Contributor Author

samsouthardjr commented Nov 10, 2021 via email

@mvorisek
Copy link
Copy Markdown
Member

these two lines from your example: https://github.com/atk4/data/compare/fix_upd_join_w_alias#diff-92f78239a1e95b6ddebce64dec3b75d71cfa81ee4073bb544f9f8f491a37218cR700-R701

create only parent and child table names only, add 3rd model with jointable

you can fork the branch from my experiment and you can see the actual queries executed - https://github.com/atk4/data/runs/4166653222?check_suite_focus=true

@samsouthardjr
Copy link
Copy Markdown
Contributor Author

samsouthardjr commented Nov 10, 2021 via email

@mvorisek
Copy link
Copy Markdown
Member

Oh, I think I see the confusion. Jointable refers to a database table, not a separate model. Isn’t that the syntax for $this->join(), referring to a table, not a model?

yes, table, but we need a model to create the table using migrator...

@mvorisek
Copy link
Copy Markdown
Member

closing in favor of #784 , thank you for the proposed solution, the dealiasing condition is correct

@mvorisek mvorisek closed this Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants