doctrine と 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 でこれをどのように実装できますか?