Ad

OctoberCMS Component Causes A Memory Error

- 1 answer

I made a component in OctoberCMS and it causes a memory error. The component has just a quite simple function. I don't know why it causes such an error.

I've edited the memory_limit up to 1024M in php.ini but nothing changed.

Component file

<?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;
  }
}
?>

I put this component in the aside of partials and this error shows in every page.

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

Comment model file

<?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';
}
Ad

Answer

I think the problem is with a query, you are trying to use. Try this instead of your query.

public function loadComments() {

 $query = Comment::query();

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

 }

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

And yes, as your are trying to get limited items from the table, Don't forget to set OrderBy(), so that you can get your desired result.

Ad
source: stackoverflow.com
Ad