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

NTDB joining tables on different key as primary key #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

NTDB joining tables on different key as primary key #114

wants to merge 3 commits into from

Conversation

apincik
Copy link

@apincik apincik commented Dec 30, 2015

Table 'e_country'

id (int)
e_country_code (varchar(2))
name (varchar(50))

Indexes: PK(id), UNIQUE(e_country_code)

Second table:

Table 'p_product'
id (int)
country (varchar(2))

Indexes: PK(id), INDEX(country)
FK: country -> e_country(e_country_code)

1:N relation
For example calling:

$this->dbContext->table("p_product")->select("p_product.*, country.e_country_code AS c_title");

The result:

SELECT `p_product`.*, `country`.`e_country_code` AS `c_title`
FROM `p_product`
LEFT JOIN `e_country` `country` ON `p_product`.`country` = `country`.`id`

If calling ref() on activeRow, the result in where clause is

`id` IN ("string", "string", "...")

Now, it gets p_product foreign keys from schema and replaces primary_key with column name on referenced table. So finall result will be:

LEFT JOIN `e_country` `country` ON `p_product`.`country` = `country`.`e_country_code`
...
`e_country_code` IN ("string", "string", "...") //ActiveRow->ref()

…eRow, it does lookup for foreign key column name in referenced table, not just primary key of referenced table
…on ActiveRow, it does lookup for foreign key column name in referenced table, not just primary key of referenced table"

This reverts commit ec9ef62.
…ow, it does lookup for foreign key column name in referenced table, not just primary key of referenced table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant