我正在重建laravel中的项目,我当前的问题是定义一个复杂的自引用hasManyThrough与表别名和SQL计算的关系。
此关系应根据匹配标记的向下总和找到相关商家。模型具有的标签越多,它们就越相关。
到目前为止,这么好。在我的旧项目中,我刚刚写下了以下合适的SQL查询:
SELECT source_merchant.id, target_merchant.id, COUNT(target_merchant.id) /
((
(SELECT COUNT(*) FROM tagged WHERE model = 'Merchant' AND model_id = source_merchant.id) +
(SELECT COUNT(*) FROM tagged WHERE model = 'Merchant' AND model_id = target_merchant.id)
) /2 ) as similarity
FROM merchants source_merchant
LEFT JOIN tagged source_merchant_tags ON (
source_merchant.id = source_merchant_tags.model_id AND
source_merchant_tags.model = 'Merchant'
)
INNER JOIN tagged target_merchant_tags ON (
source_merchant_tags.tag_id = target_merchant_tags.tag_id
AND (source_merchant_tags.model = 'Merchant' AND target_merchant_tags.model = 'Merchant')
AND (source_merchant_tags.model_id != target_merchant_tags.model_id)
)
LEFT JOIN merchants target_merchant ON (
target_merchant_tags.model_id = target_merchant.id AND target_merchant_tags.model = 'Merchant'
)
WHERE source_merchant.id = 2
GROUP BY source_merchant.id, target_merchant.id
ORDER BY similarity DESC
LIMIT 5
最好是抓住像
这样的东西public function related_merchants() {
return $this->hasManyThroug(relations_stuff_i_cannot_imagine...)
->selectRaw("SELECT source_merchant.id, target_merchant.id, COUNT(target_merchant.id) /
((
(SELECT COUNT(*) FROM tagged WHERE model = 'Merchant' AND model_id = source_merchant.id) +
(SELECT COUNT(*) FROM tagged WHERE model = 'Merchant' AND model_id = target_merchant.id)
) /2 ) as similarity")
->groupBy('source_merchant.id', 'target_merchant.id ')
->orderBy('similarity')
->limit(5);
}
那就是:-)不幸的是,我找不到解决方案,因为我不知道,如何在hasManyThrough()
中定义合适的关系参数...
修改 - 尝试将Laravel查询构建为suggested:
public function getRelatedMerchantsAttribute() {
return $this->from('merchants AS source_merchant')
->selectRaw('source_merchant.id, target_merchant.id, COUNT(target_merchant.id) /
((
(' . DB::table('tagged')->whereRaw("model = 'Merchant' AND model_id = source_merchant.id")->count() . ') +
' . DB::table('tagged')->whereRaw("model = 'Merchant' AND model_id = target_merchant.id")->count() . ')
/2 ) AS similarity')
->lefJoin('tagged AS source_merchant_tags', function ($join) {
$join->on('source_merchant.id', '=', 'source_merchant_tags.model_id')
->on('source_merchant_tags.model', '=', 'Merchant');
})
->join('tagged AS target_merchant_tags', function ($join) {
$join->on('source_merchant_tags.tag_id', '=', 'target_merchant_tags.tag_id')
->on('source_merchant_tags.model', '=', 'Merchant')
->on('target_merchant_tags.model', '=', 'Merchant')
->on('source_merchant_tags.model_id', '!=', 'target_merchant_tags.model_id');
})
->leftJoin('merchants AS target_merchant', function ($join) {
$join->on('target_merchant_tags.model_id ', '=', 'target_merchant.id')
->on('target_merchant_tags.model', '=', 'Merchant');
})
->whereRaw('source_merchant.id = ?', [ $this->id ])
->groupBy('source_merchant.id', 'target_merchant.id ')
->orderBy('similarity')
->limit(5)
->get();
}
由于以下原因,此解决方案尚未运行:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'merchants.id'
in 'where clause' (SQL: select count(*) as aggregate from `tagged`
where model = 'Merchant' AND model_id = merchants.id)
修改 - 使用this query会产生以下Laravel错误:
SQLSTATE [HY093]:参数号无效。 SQL:
select source_merchant.id, target_merchant.id,
COUNT(target_merchant.id) /
((
(select COUNT(*) from `tagged` where `model` = Merchant and `model_id` = source_merchant.id) +
(select COUNT(*) from `tagged` where `model` = Merchant and `model_id` = target_merchant.id)
) /2 ) AS similarity
from `merchants` as `source_merchant`
left join `tagged` as `source_merchant_tags` on
`source_merchant`.`id` = `source_merchant_tags`.`model_id`
and `source_merchant_tags`.`model` = Merchant
inner join `tagged` as `target_merchant_tags` on
`source_merchant_tags`.`tag_id` = `target_merchant_tags`.`tag_id`
and `source_merchant_tags`.`model` = Merchant
and `target_merchant_tags`.`model` = 2
and `source_merchant_tags`.`model_id` != `target_merchant_tags`.`model_id`
left join `merchants` as `target_merchant` on
`target_merchant_tags`.`model_id` = `target_merchant`.`id`
and `target_merchant_tags`.`model` = ?
where `source_merchant`.`id` = ?
group by `source_merchant`.`id`, `target_merchant`.`id`
order by `similarity` desc
limit 5
我甚至想知道为什么在这里使用当前的模型ID:and target_merchant_tags.model = 2
...
看一下这个截图。条件参数'商家'成为当前的模型ID,在这种情况下它是2(所选文本)。红色圆圈中的两个参数保持为空。这里有什么问题?
答案 0 :(得分:1)
我不认为关系是你情况的正确选择。在Laravel / Eloquent意义上,源和目标商家并不真正相关,因为常用标签仅用于确定订单。
我只是将原始SQL转换为Laravel查询,并将您的方法重命名为getRelatedMerchantsAttribute
。然后,您可以使用$merchant->relatedMerchants
。
尝试此查询:
$sourceCount = DB::table('tagged')
->selectRaw('COUNT(*)')
->where('model', 'Merchant')
->where('model_id', DB::raw('source_merchant.id'));
$targetCount = DB::table('tagged')
->selectRaw('COUNT(*)')
->where('model', 'Merchant')
->where('model_id', DB::raw('target_merchant.id'));
$this->from('merchants AS source_merchant')
->addBinding($sourceCount->getBindings(), 'select')
->addBinding($targetCount->getBindings(), 'select')
->selectRaw('source_merchant.id, target_merchant.id, COUNT(target_merchant.id) /
((
(' . $sourceCount->toSql() . ') +
(' . $targetCount->toSql() . '))
/2 ) AS similarity')
->leftJoin('tagged AS source_merchant_tags', function ($join) {
$join->on('source_merchant.id', '=', 'source_merchant_tags.model_id')
->where('source_merchant_tags.model', '=', 'Merchant');
})
->join('tagged AS target_merchant_tags', function ($join) {
$join->on('source_merchant_tags.tag_id', '=', 'target_merchant_tags.tag_id')
->where('source_merchant_tags.model', '=', 'Merchant')
->where('target_merchant_tags.model', '=', 'Merchant')
->on('source_merchant_tags.model_id', '!=', 'target_merchant_tags.model_id');
})
->leftJoin('merchants AS target_merchant', function ($join) {
$join->on('target_merchant_tags.model_id', '=', 'target_merchant.id')
->where('target_merchant_tags.model', '=', 'Merchant');
})
->where('source_merchant.id', $this->id)
->groupBy('source_merchant.id', 'target_merchant.id')
->orderByDesc('similarity')
->limit(5)
->get();