OctoberCMS组件导致内存错误

OctoberCMS component causes a memory error

我在OctoberCMS 中制作了一个组件,它导致了内存错误。 该组件只有一个非常简单的功能。 不知道为什么会报这样的错误

我在 php.ini 中将 memory_limit 编辑到 1024M,但没有任何改变。

组件文件

<?php
namespace Jiwon\Byapps\Components;

use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use Exception;
use Jiwon\Byapps\Models\Comment;

class Comments extends ComponentBase
{
  public $comments;

  public function componentDetails()
  {
      return [
          'name'        => 'Comment List',
          'description' => 'comment list'
      ];
  }

  public function defineProperties()
  {
      return [
          'display' => [
              'title'       => 'number of the comments',
              'description' => 'number of the comments list',
              'default'     => 10,
              'validationPattern' => '^[0-9]+$',
              'validationMessage' => 'only number'
          ],
      ];
  }

  public function onRun()
  {
     $this->comments = $this->loadComments();
  }

  protected function loadComments() {

     $query = Comment::all();

     if ($this->property('display') > 0) {
       $query = $query->take($this->property('display'));

     }

     return $query;
  }
}
?>

我把这个组件放在partials的旁边,每个页面都会出现这个错误。

Allowed memory size of 134217728 bytes exhausted (tried to allocate 8192 bytes) /home/ljw/public_html/byapps_cms/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php line 290

评论模型文件

<?php namespace Jiwon\Byapps\Models;

use Model;

class Comment extends Model
{
    use \October\Rain\Database\Traits\Validation;

    public $timestamps = false;

    public $connection = 'byapps';
    public $table = 'BYAPPS_comment_data';
}

我认为问题出在您正在尝试使用的查询上。试试这个而不是你的查询。

public function loadComments() {

 $query = Comment::query();

 if (!empty($this->property('display'))) {
   $query = $query->limit($this->property('display'));

 }

 $query = $query->get();
 return $query;
}

是的,当您尝试从 table 中获取有限的项目时,请不要忘记设置 OrderBy(),这样您就可以获得您想要的结果。