我试图在学说和symfony中实现贝叶斯平均逻辑。
我有这样的基本原生Mysql查询:
Create View `ratings` AS
SELECT
restaurant_id,
(SELECT count(restaurant_id) FROM ratings) / (SELECT count(DISTINCT restaurant_id) FROM ratings) AS avg_num_votes,
(SELECT avg(rating) FROM ratings) AS avg_rating,
count(restaurant_id) as this_num_votes,
avg(rating) as this_rating
FROM
ratings
GROUP BY
restaurant_id
SELECT
restaurant_id,
((avg_num_votes * avg_rating) + (this_num_votes * this_rating)) / (avg_num_votes + this_num_votes) as real_rating
FROM `ratings`
此查询从我们检索记录的位置创建表视图。
从一些文件中我了解到我们无法在Doctrine中创建视图。所以另一种选择是创建临时表。我们如何创建具有不同结构的临时表。
参考:http://shout.setfive.com/2013/11/21/doctrine2-using-resultsetmapping-and-mysql-temporary-tables/
// Add the field info for each column/field
foreach( $classMetadata->fieldMappings as $id => $obj ){
$rsm->addFieldResult('u', $obj["columnName"], $obj["fieldName"]);
// I want to crate new fields to store avg rating etc.
}
我们如何在Doctrine和Symfony中实现它?