1

I can't get Table class to work in a controller. I try to get the data of a record in a table but I always get

0 Call to a member function load() on bool

Is it possible to use the Table class in a controller? What could be omitting?

administrator/components/com_aaprueba/src/Table/EspaciosTable.php

namespace Sfi\Component\Aaprueba\Administrator\Table;

defined('_JEXEC') or die;

use \Joomla\CMS\Table\Table;
use \Joomla\Database\DatabaseDriver;

class EspaciosTable extends Table
{
    public function __construct(DatabaseDriver $db)
    {
        parent::__construct('#__sfi_es_espacios', 'id', $db);
    }
}

components/com_aaprueba/src/Controller/EspaciosController.php

namespace Sfi\Component\Aaprueba\Site\Controller;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Table\Table;

class EspaciosController extends FormController
{
    public function getEspacio()
    {
        Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_aaprueba/src/Table');
        $table = Table::getInstance('Espacios', 'Table', array());
        $id = 2;
        $table->load($id);

    }
}

1 Answer 1

2

Use the MVC factory. Joomla\CMS\Table\Table::addIncludePath() call is not needed in J4 style components.

$table = $this->factory->createTable('Espacios', 'Administrator');
1
  • Thanks!. Out of model or controller like helpers or Ajax controllers, I found @Sharky solution how help me: $table = \Joomla\CMS\Factory::getApplication()->bootComponent('com_foo')->getMVCFactory()->createTable('user', 'Administrator');
    – Peli
    Commented Jan 4, 2023 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.