Using entity with composite key depending on other relation #6087

Open
opened 2026-01-22 15:26:33 +01:00 by admin · 0 comments
Owner

Originally created by @tobearlabs on GitHub (Oct 17, 2018).

Database-Scenario:

Entity: Company (id (PK),name)
Entity: Product (id (PK),title)
Entity: ProductPrice (company_id (PK),product_id(PK), price)

Each company can have multiple Products and each Product can be assigned to multiple companies (N-M).
Each Product can have multiple Prices (for each company) and every Price has ONE product (1-N).

In other words:
Companies using (partially) the same product data but with different prices for each product.

From the database perspective everything looks fine after creating the database schema. (described above)

Q A
Version 2.6

My aim is to load now one company with the related products and the regarding company-prices for the products like:

$company->find($id)->getProducts() => now the products are correctly fetched only for the company.

Looping now through the received products to get only the company price for each product, like:
$product->getPrices() => the price are not be filtered by company anymore, only for the product, so i get a list of all prices for every company. (composite PK is set)

Is there a way that doctrine filters the product-prices automatically when using the "ususal" way to get the company object? I can´t find any annotations that the product-price relation is not only based on product_id, company_id should be considered also (from the first call). Like company->products[0]->price has the relation between the "tree".

Writing a new Repository-method with left joins to load all in one works fine, but it would be cleaner from my perspective getting the company and using the default getters for products and prices would deliver the same result. Where is my (brain)-failure happening :-) ?

Originally created by @tobearlabs on GitHub (Oct 17, 2018). Database-Scenario: Entity: Company (id (PK),name) Entity: Product (id (PK),title) Entity: ProductPrice (company_id (PK),product_id(PK), price) Each company can have multiple Products and each Product can be assigned to multiple companies (N-M). Each Product can have multiple Prices (for each company) and every Price has ONE product (1-N). In other words: Companies using (partially) the same product data but with different prices for each product. From the database perspective everything looks fine after creating the database schema. (described above) | Q | A |------------ | ----- | Version | 2.6 My aim is to load now one company with the related products and the regarding company-prices for the products like: $company->find($id)->getProducts() => now the products are correctly fetched only for the company. Looping now through the received products to get only the company price for each product, like: $product->getPrices() => the price are not be filtered by company anymore, only for the product, so i get a list of all prices for every company. (composite PK is set) Is there a way that doctrine filters the product-prices automatically when using the "ususal" way to get the company object? I can´t find any annotations that the product-price relation is not only based on product_id, company_id should be considered also (from the first call). Like company->products[0]->price has the relation between the "tree". Writing a new Repository-method with left joins to load all in one works fine, but it would be cleaner from my perspective getting the company and using the default getters for products and prices would deliver the same result. Where is my (brain)-failure happening :-) ?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6087