首页 > 解决方案 > OctoberCMS 组件导致内存错误

问题描述

我在 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的旁边,这个错误显示在每个页面中。

134217728字节的允许内存大小用尽(试图分配8192字节)/home/ljw/public_html/byapps_cms/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php第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';
}

标签: phpmysqllaraveloctobercms

解决方案


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

public function loadComments() {

 $query = Comment::query();

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

 }

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

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


推荐阅读