GithubHelp home page GithubHelp logo

Comments (3)

vjik avatar vjik commented on June 16, 2024

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();

from db.

Tigrov avatar Tigrov commented on June 16, 2024

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]]')])

from db.

vjik avatar vjik commented on June 16, 2024

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.

from db.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.