PHP-Doctrine2:项目 - 商店 - ItemsAtShops - 如何方便地使用Doctrine2? [英] PHP-Doctrine2: Items - Shops - ItemsAtShops - how to conveniently implement using Doctrine2?
问题描述
我不知道如何实现以下关系
使用Doctrine 2语法:
Somehow I can't figure out how to implement the following relations using Doctrine 2 syntax:
我有项目和商店。每个商品在每个商店都有不同的价格和不同的
数量。所以,我有Items表,商店表和
ItemsAtShops表。我如何反映最后一个在Doctrine?
I have Items and Shops. Each item has different price and different quantity at each shop. So, I have Items table, Shops table and ItemsAtShops table. How do I reflect the last one in Doctrine?
我想我需要创建ItemsAtShops实体,涉及ManyToOne - >
项目和ManyToOne - >商店,右?但在这种情况下...如何
方便地在特定商店获取商品列表,其中的
价格和数量在给定商店?那么所有这些都可以方便地重复
?
I guess I need to create ItemsAtShops entity, relates ManyToOne -> Items and ManyToOne -> Shops, right? But in this case... how do I conveniently fetch a list of Items at the specific Shops with their prices and quantities at given Shops? So, that all these can be conveniently iterated?
我需要提交一个包含项目列表和价格的页面,
数量具体店铺。有一个模板显示项目
在那里(所有它的子属性 - 价格等)。所以,最简单的方法是将一个对象传递给
这个模板并且遍历它,它是subobjects(如果有的话)。
I need to render a page with a list of Items and their Prices and Quantities at specific shops. There is a template for displaying Item there (with all it's subproperties - prices etc). So, it would be the most convenient to pass just one object to this template and iterate it and it's subobjects (if any) there.
推荐答案
我也在Doctrine中与这种场景斗争。 Rails已经把他们的 has_many:through =>
关系弄坏了,这使得这种事情变得微不足道。
I struggle with this kind of scenario in Doctrine, as well. Rails had spoiled me with their has_many :through =>
relationship which makes this sort of thing trivial.
你是正确的,你需要三个实体:商店,项目,ItemsAtShops使用双重ManyToOne关系。
You are correct, you do need three entities: Shops, Items, ItemsAtShops using dual ManyToOne relationships.
我会认为ItemsAtShop将看起来像:
I would assume that ItemsAtShop would look like:
class ItemsAtShop
{
private $shop;
private $items;
private $quantity;
}
尽管如此,您需要摇滚连接: / p>
As far a querying goes, you'll need to rock the joins:
$queryBulder->select('ias')
->from(ItemsAtShop, 'ias')
->leftJoin('ias.Item', 'i')
->leftJoin('ias.Shop', 's')
->where('s.id = :shop_id')
->andWhere('i.price <= :my_price')
->orderBy('i.price', 'DESC');
记住,当使用DQL时,通常会查询整个Entity对象,这将会获取他们的关系他们自己的。这可能导致收集 ItemsAtShop
,您可以迭代。
Remember, when using DQL, you're usually querying entire Entity objects, which will fetch their relationships on their own. This should result in a collection of ItemsAtShop
you can iterate over.
我可能会采取一些困惑,找出正确查询。我经常从我知道的SQL查询开始,并将其转换为DQL。
I might take some fidgeting to figure out the correct query. I'll often start with a SQL query I know works, and translate it into DQL.
这篇关于PHP-Doctrine2:项目 - 商店 - ItemsAtShops - 如何方便地使用Doctrine2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!