我使用doctrine 1.2,这是我的架构:
ProviderProduct:
columns:
provider_id:
type: integer
primary: true
product_id:
type: integer
primary: true
num:
type: integer
default: 0
unsigned: true
Provider:
columns:
name: {type: string(255), notnull: true, notblank: true, unique: true}
relations:
Products:
class: Product
local: provider_id
foreign: product_id
refClass: ProviderProduct
Product:
columns:
#a lot of columns
relations:
Providers:
class: Provider
local: product_id
foreign: provider_id
refClass: ProviderProduct
我想要的是获得特定提供商的max num(这是ammount)产品。 我试过这个问题:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.Providers pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('ProviderProduct.num');
$query->fetchOne();
结果sql查询:
SELECT p.* FROM product p
INNER JOIN provider_product p3 ON (p.id = p3.product_id)
INNER JOIN provider p2 ON p2.id = p3.provider_id, provider_product p4
WHERE (p2.name = ?) ORDER BY p4.num
正如你所看到的那样,它不按num字段排序。那么,对我的任务来说什么是正确的dql?
答案 0 :(得分:0)
How order doctrine query for many-to-many relationship?
您可以使用以下构造:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.Providers pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('p.Providers.ProviderProduct.num');
Doctrine 1的非原生行为是为多对多关系创建一个不可见的别名。您可以使用此别名来构建正确的顺序。
答案 1 :(得分:0)
我认为您还需要关联refClass:
ProviderProduct:
columns:
provider_id:
type: integer
primary: true
product_id:
type: integer
primary: true
num:
type: integer
default: 0
unsigned: true
relations:
Provider: { foreignAlias: ProviderProduct }
Product: { foreignAlias: ProviderProduct }
然后通过reffClass联系获取数字:
$query = Doctrine_Query::create()->select('p.*')
->from('Product p')
->innerJoin('p.ProviderProduct pp')
->innerJoin('pp.Provider pr')
->where('pr.name = ?', 'WhiteStore')
->orderBy('pp.num DESC') // to get de max first
->groupBy('p.id') // if any duplicates
->limit(1);
$product = $query->fetchOne();