Joomla 1.6 JCategories::get() method produces 'PHP Fatal error: Allowed memory exhausted' in custom MVC component

醉酒当歌 提交于 2019-12-10 22:04:11

问题


I'm implementing a custom MVC component following the Joomla 1.6 Documentation.

I am running into a problem when trying to use JCategories::get() to obtain a list of categories and their children from com_component. I receive the following error:

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

If I do not issue a print_r($this->items); to list the items, I do not receive an error. If I change the line

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

to read

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

I do not receive the error.

I have included all of my custom component code below. Just as an FYI, I've spent the past couple of days in irc.freenode.net/#joomla speaking with anyone willing to help with very little progress made. Any help would be much appreciated.

Controller code:

  <?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();

Model code:

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

}

View code:

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

    }

}

Template code:

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

回答1:


I found that attempting to var_dump() or print_r() JCategoryNode results in an endless loop. Therefore, I modified my model above to the following:

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

    }

}


来源:https://stackoverflow.com/questions/6269873/joomla-1-6-jcategoriesget-method-produces-php-fatal-error-allowed-memory-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!