我正在按照Joomla 1.6文档实现一个自定义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('banners');
我没有收到错误。
我在下面包含了我所有的自定义组件代码。仅供参考,在过去的几天里,我在irc.freenode.net/#joomla与任何愿意在进展甚微的情况下提供帮助的人交谈。任何帮助都将不胜感激。
控制器代码:
<?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;
}
}