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

Joomla! 4.x Coding • How to code item model getTable()?

$
0
0
Something obvious I'm sure but I'm really struggling to understand this.

I have an item model (src/Models/ItemModel) where I am trying to get the Table working.

The relevant code in src/Models/ItemModel.php is

Code:

<?phpnamespace Acme\Component\Example\Administrator\Model;use Joomla\CMS\Factory;use Joomla\CMS\Language\Text;use Joomla\CMS\MVC\Model\AdminModel;use Joomla\CMS\Table\Table;use Joomla\CMS\Versioning\VersionableModelTrait;use Joomla\Database\DatabaseDriver;use Joomla\Database\Exception\DatabaseExceptionInterface;// Prevent direct access// phpcs:disable PSR1.Files.SideEffects\defined('_JEXEC') or die;// phpcs:enable PSR1.Files.SideEffectsclass ItemModel extends AdminModel{       public function getTable($type = 'Item', $prefix = 'Acme\\Component\\Example\\Administrator\\Table', $config = [])    {        /* All of these give the error "Failed to create table instance for type Item.":                $table = Table::getInstance('Item', 'Acme\\Component\\Example\\Administrator\\Table', $config);        $table = Table::getInstance('Item', 'Administrator', $config);                $table = $this->getMVCFactory()->createTable('Item', 'Acme\\Component\\Example\\Administrator\\Table', $config);        $table = $this->getMVCFactory()->createTable('Item', '\\Acme\\Component\\Example\\Administrator\\Table', $config);        $table = $this->getMVCFactory()->createTable('Item', 'Administrator');        */        $table = Factory::getApplication()->bootComponent('com_acme_example')->getMVCFactory()->createTable('Item', 'Administrator');        if (!$table)            throw new \RuntimeException(sprintf('Failed to create table instance for type %s.', $type));        file_put_contents('/tmp/debug.log', Factory::getDate()->toSql() . ":created table" . PHP_EOL, FILE_APPEND);        return $table;    }...}
From all the examples I've read this should work:

Code:

$table = $this->getMVCFactory()->createTable('Item', 'Administrator');
because I'm 'in' a model. But it doesn't, it returns false.
This code does 'work', in that it returns something:

Code:

$table = Factory::getApplication()->bootComponent('com_acme_example')->getMVCFactory()->createTable('Item', 'Administrator');
but I don't understand why I need to 'boot' the component.

Anyone know what's going on here? Or what this code is supposed to look like?

Statistics: Posted by david0058 — Mon Dec 23, 2024 11:31 am



Viewing all articles
Browse latest Browse all 1635

Trending Articles



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