php - 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';
}
解决方案
我认为问题在于您正在尝试使用的查询。试试这个而不是你的查询。
public function loadComments() {
$query = Comment::query();
if (!empty($this->property('display'))) {
$query = $query->limit($this->property('display'));
}
$query = $query->get();
return $query;
}
是的,当你试图从表中获取有限的项目时,不要忘记设置 OrderBy(),这样你就可以获得你想要的结果。
推荐阅读
- docker - Docker Compose 通过 SSH 失败
- python - 获取错误 AttributeError: 'datetime.datetime' 对象在尝试添加一些日期时间时没有属性 'timedelta'
- sqlite - 将 Nesper 连接到外部数据库 PgSQL/SQLite
- synchronization - 如何在 p2p 游戏的 2 个对等方之间同步时钟
- google-cloud-platform - 验证模型从 Cloud Scheduler 上传到 VertexAI 作业
- python - 通过 django SMTP 发送电子邮件而不在刷新时发送表单数据?
- docusignapi - 如何使用 C# DocuSign 的 API 查看一批信封的自定义字段
- ios - 对于我的电影观看列表应用程序,可以将电影海报图像存储在设备上吗?
- javascript - IPFSAccessController.save 错误:错误:已弃用,使用 .toString()
- swift - 如何从 XCUITest 中的 MenuItem 读取值?