Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 1468

Joomla! 4.x Coding • Re: How to fix error 00 Call to a member function get() on null ($listOrder)

$
0
0
Looks like the line $this->state->get('list-ordering') is at fault, which means that $this->state is null. I see you've got get('State') in your view, but it's not working. You need "populateState" in your model.
Thanks for your answer, I did some modifications of code.

Model:

Code:

<?phpnamespace Joomla\Component\Faq\Administrator\Model;use Joomla\CMS\Factory;use Joomla\CMS\MVC\Model\ListModel;defined('_JEXEC') or exit();class FaqContentModel extends ListModel {/** * Конструктор. * @param   array  $config  Массив с конфигурационными параметрами. */public function __construct($config = []){// Добавляем валидные поля для фильтров и сортировки.$config['filter_fields'] = array(            'id', 'a.id',            'title', 'a.title',            'ordering', 'a.ordering',            'state', 'a.state',            'publish_up', 'a.publish_up'        );parent::__construct($config);}/**     * Method to get a list of items.     *     * @return  mixed   An array of data items on success, false on failure.     */    public function getItems(){    // Get the query for retrieving items    $query = $this->getQuery();    // Set the query limits and execute    $this->setState('list.limit', 0);    $this->setState('list.start', 0);    $this->setQuery($query);    // Get the items from the query using parent::getItems()    $items = parent::getItems();    return $items;}/** * Метод для построения SQL запроса для загрузки списка данных. * @return  string  SQL запрос. */protected function getQuery(): string{$db = $this->getDbo();        $query = $db->getQuery(true);$query->select('*');$query->from('#__faq_item');        /* Add Searching */$state = $this->getState('filter.state');if (is_numeric($state)){$query->where('state = ' . (int) $state);}elseif ($state === ''){$query->where('(state = 0 OR state = 1)');}$search = $this->getState('filter.search');if (!empty($search)){$search = $db->quote('%' . $db->escape($search, true) . '%', false);$query->where('title LIKE ' . $search);}        $published = $this->getState('filter.published');        if (is_numeric($published)) {                $query->where($db->quoteName('published') . ' = ' . $db->quote($published));        } elseif ($published === '') {                $query->whereIn($db->quoteName('published'), array(0, 1));        }// Добавляем сортировку.$orderCol  = $this->state->get('list.ordering', 'id');$orderDirn = $this->state->get('list.direction', 'desc');$query->order($db->escape($orderCol . ' ' . $orderDirn));return $query;}protected function populateState($ordering = 'a.id', $direction = 'desc'){    $app = Factory::getApplication();    $input = $app->input;    $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');    $this->setState('filter.search', $search);    $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '');    $this->setState('filter.published', $published);    // Set the default ordering and direction if not already set    parent::populateState($ordering, $direction);}/* Prepare a faqcontentiten record for saving in the database */protected function prepareTable($table){// Set ordering to the last item if not setif (empty($table->ordering)){$db = $this->getDbo();$query = $db->getQuery(true)->select('MAX(ordering)')->from('#__faq_item');$db->setQuery($query);$max = $db->loadResult();$table->ordering = $max + 1;}}}
HtmlView.php

Code:

<?phpnamespace Joomla\Component\Faq\Administrator\View\FaqContent;defined('_JEXEC') or die;use Joomla\CMS\Language\Text;use Joomla\CMS\Toolbar\ToolbarHelper;use Joomla\CMS\Factory;use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;class HtmlView extends BaseHtmlView {        protected $items;    protected $pagination;    protected $state;    public $filterForm;    public $activeFilters;         public function display($tpl = null) {        try {            // Get model            $model = $this->getModel('FaqContent', 'Joomla\Component\Faq\Administrator\Model', array('ignore_request' => true));                // Check if the model is properly loaded            if (!$model) {                throw new \Exception('Failed to load model.');            }                // Set state variables            $model->populateState();                // Get items from the model            $this->items = $model->getItems();            $this->pagination = $model->getPagination();            $this->state = $model->getState();                // Get filter form and active filters            $this->filterForm = $model->getFilterForm();            $this->activeFilters = $model->getActiveFilters();                // Add toolbar            $this->addToolBar();                // Display the view            parent::display($tpl);                // Set document title            $this->setDocument(Factory::getDocument());        } catch (\Exception $e) {            // Handle any exceptions            Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');        }    }        protected function addToolBar() {        ToolbarHelper::title(Text::_("COM_FAQ_LIST"), 'question-circle');        ToolbarHelper::addNew('faqcontentitem.add');        ToolbarHelper::editList('faqcontentitem.edit');        ToolbarHelper::publish('faqcontentitem.publish', 'JTOOLBAR_PUBLISH', true);        ToolbarHelper::unpublish('faqcontentitem.unpublish', 'JTOOLBAR_UNPUBLISH', true);        ToolbarHelper::deleteList('', 'faqcontentitem.delete');    }    public function setDocument(\Joomla\CMS\Document\Document $document): void {        $document->setTitle(Text::_("COM_FAQ"));    }}
Controller:

Code:

<?phpnamespace Joomla\Component\Faq\Administrator\Controller;use Joomla\CMS\MVC\Controller\AdminController;use Joomla\CMS\MVC\Factory\MVCFactoryInterface;use Joomla\CMS\Application\CMSApplication;use Joomla\Input\Input;defined('_JEXEC') or die;class FaqContentController extends AdminController {        public function __construct($config = [], MVCFactoryInterface $factory = null, $app = null, $input = null)    {        parent::__construct($config, $factory, $app, $input);    }    public function getModel($name = 'FaqContent', $prefix = 'Administrator', $config = ['ignore_request' => true]){        return parent::getModel($name, $prefix, $config);    }}
And I see these errors:
errors.png
Where I have mistake?

Statistics: Posted by zeus07 — Tue Mar 26, 2024 9:48 pm



Viewing all articles
Browse latest Browse all 1468

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>