【发布时间】:2020-10-12 13:37:06
【问题描述】:
我有一类有用户评分的新闻。我的目标是找到所有评分的平均值,我使用的是 MongoDB。我试图找到一个平均内置的功能,但我找不到。所以我打算 使用 count() 函数,然后获取数组集合中所有元素的总和,然后将总和除以计数,但是总和效果不佳,是否有内置函数?如果不是,我如何迭代 PHP 数组集合并添加值
public function __construct()
{
$this->positiveFeedback = new ArrayCollection();
$this->negativeFeedback = new ArrayCollection();
$this->rating = new ArrayCollection();
}
/**
* @return Collection|Rating[]
*/
public function getRating(): Collection
{
return $this->rating;
}
public function addRating(Rating $rating): self
{
if (!$this->rating->contains($rating)) {
$this->rating[] = $rating;
$rating->setNews($this);
}
return $this;
}
public function removeRAting(Rating $rating): self
{
if ($this->rating->contains($rating)) {
$this->rating->removeElement($rating);
// set the owning side to null (unless already changed)
if ($rating->getNews() === $this) {
$rating->setNews(null);
}
}
return $this;
}
/**
* Get the value of RatingAverage
*/
public function getRatingAverage()
{
$ratingsNumber = $this->getRating()->count();
$ratingSum = $this->getRating()->getValues();
}
getRatingAverage 是我卡住的地方
【问题讨论】:
标签: php mongodb symfony average