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

Query::leftJoin - a simpler way to quote join column? #754

Open
arogachev opened this issue Sep 22, 2023 · 3 comments
Open

Query::leftJoin - a simpler way to quote join column? #754

arogachev opened this issue Sep 22, 2023 · 3 comments

Comments

@arogachev
Copy link
Contributor

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;

/**
 * @var ConnectionInterface $database 
 * @var string $tableName
 * @var string $childrenTableName
 */
$quoter = $database->getQuoter();
$quotedJoinColumn = $quoter->quoteTableName($tableName)  . '.' . $quoter->quoteColumnName('name');
$rawItems = (new Query($database))
    ->select($tableName . '.*')
    ->from($tableName)
    ->leftJoin($childrenTableName, [$childrenTableName . '.child' => new Expression($quotedJoinColumn)])
    ->all();
@vjik
Copy link
Member

vjik commented Sep 22, 2023

You can use so code for quote:

$quotedJoinColumn =  $quoter->quoteColumnName($tableName . '.name');

quoteColumnName() supports column names with table.

And simpler way will be so:

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;

/**
 * @var ConnectionInterface $database 
 * @var string $tableName
 * @var string $childrenTableName
 */
$quoter = $database->getQuoter();
$quotedJoinColumn =  $quoter->quoteColumnName($tableName . '.name');
$rawItems = (new Query($database))
    ->select($tableName . '.*')
    ->from($tableName)
    ->leftJoin(
		$childrenTableName, 
		[$childrenTableName . '.child' => new Expression($quotedJoinColumn) ]
	)
    ->all();

@Tigrov
Copy link
Member

Tigrov commented Sep 23, 2023

This could be done by one of the following ways:

  1. By default for ...Join() methods process condition of associative array as "column name" => "column name",
    e.g. ['parent.id' => 'child.parent_id'] quote key and value of the associative array as column names.
    Use Expression or parameters if need to pass value (not "column name"),
    e.g. ['parent.id' => 'child.parent_id', 'child.has_parent' => new Expression('1'), 'child.has_parent' => ':param']

  2. Add one more condition type, e.g. column: ['column', 'parent.id' => 'child.parent_id']
    and quote key and value as column names.
    This condition can be used in other places (not only in ...Join()),
    e.g. ->where(['column', 'parent.id' => 'child.parent_id'])

  3. Add specific class of ExpressionInstance e.g. ColumnName: ['parent.id' => new ColumnName('child.parent_id')]
    and quote it as column name. It also can be used in other palces (where(), having(), etc)

Also there is way
->leftJoin('child', ['parent.id' => new Expression('{{child}}.[[parent_id]]')])

@vjik
Copy link
Member

vjik commented Nov 5, 2023

In current implementation to $on bind params and used result string. And this OK for me, $on is not so often be a column name.

3th way from @Tigrov looks fine solution for this case.

@Tigrov Tigrov added this to the 1.1.0 milestone Nov 9, 2023
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

3 participants