3

我试图在学说和 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 中实现这一点?

4

0 回答 0