gpt4 book ai didi

php - Joomla 1.6 JCategories::get() 方法在自定义 MVC 组件中生成 'PHP Fatal error: Allowed memory exhausted'

转载 作者:搜寻专家 更新时间:2023-10-31 20:52:13 24 4
gpt4 key购买 nike

我正在按照 Joomla 1.6 Documentation 实现自定义 MVC 组件.

我在尝试使用 JCategories::get()com_component 获取类别列表及其子项时遇到了问题。我收到以下错误:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 125306881 bytes)

如果我发出print_r($this->items);来列出项目,我不会收到错误.如果我换行

$categories = JCategories::getInstance('Content');

阅读

$categories = JCategories::getInstance('横幅');

没有收到错误。

我在下面包含了我所有的自定义组件代码。仅供引用,过去几天我一直在 irc.freenode.net/#joomla 与任何愿意提供帮助的人交谈,但进展甚微。任何帮助将不胜感激。

Controller 代码:

  <?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import joomla controller library
jimport('joomla.application.component.controller');

$controller = JController::getInstance('CtItem');
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();

模型代码:

<?php
// No direct access to this file
defined('_JEXEC') or die;

// import Joomla Categories library
jimport( 'joomla.application.categories' );

class CtItemModelCtItem extends JModel
{

private $_items = null;

private $_parent = null;

public function getItems($recursive = false)
{
$categories = JCategories::getInstance('Content');
$this->_parent = $categories->get(15);
if(is_object($this->_parent))
{
$this->_items = $this->_parent->getChildren($recursive);
}
else
{
$this->_items = false;
}

return $this->_items;
}

}

查看代码:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

class CtItemViewCtItem extends JView
{

// Overwriting JView display method
function display($tpl = null)
{

// Assign data to the view
$this->items = $this->get('Items');

if(count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));

return false;
}

// Display the view
parent::display($tpl);

}

}

模板代码:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();
?>

<div id="Test"><?=print_r($this->items, true)?></div>

最佳答案

我发现尝试 var_dump()print_r() JCategoryNode 会导致无限循环。因此,我将上面的模型修改为以下内容:

<?php
// No direct access to this file
defined('_JEXEC') or die;

// import Joomla Categories library
jimport( 'joomla.application.categories' );

class CtItemModelCtItem extends JModel
{

private $_items = null;

private $_parent = null;

public function getItems($recursive = false)
{
$categories = JCategories::getInstance('Content');
$this->_parent = $categories->get(15);
if(is_object($this->_parent))
{
$this->_items = $this->_parent->getChildren($recursive);
}
else
{
$this->_items = false;
}

return $this->loadCats($this->_items);
}


protected function loadCats($cats = array())
{

if(is_array($cats))
{
$i = 0;
$return = array();
foreach($cats as $JCatNode)
{
$return[$i]->title = $JCatNode->title;
if($JCatNode->hasChildren())
$return[$i]->children = $this->loadCats($JCatNode->getChildren());
else
$return[$i]->children = false;

$i++;
}

return $return;
}

return false;

}

}

关于php - Joomla 1.6 JCategories::get() 方法在自定义 MVC 组件中生成 'PHP Fatal error: Allowed memory exhausted',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com