Joomla 4 Table usage on external php script

351 views
Skip to first unread message

Alex Wai

unread,
Jun 4, 2022, 9:24:33 AM6/4/22
to Joomla! General Development
Hello,

I am trying to insert a record to the database table via external script but am getting Call to a member function bind() on bool. can anyone guide on making this inset works.

Filename: sample.php 

<?php
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

use \Joomla\CMS\Factory;
use \Joomla\CMS\Table\Table;

$container = Factory::getContainer();
$container->alias('session.web', 'session.web.site')
->alias('session', 'session.web.site')
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
Factory::$application = $app;

$input    = $app->input;

$input                    = PH::getApplication()->input;
$row                    = Table::getInstance("ItemTable", '\\Example\\Component\\Demo\\Site\\Table\\');;

$item                     = new \stdClass();
$item->id                 = '1';
$item->name             = 'Title';  
$row->bind ($item);
$row->check();
$row->store();
 




Regards
Alex

MarkRS

unread,
Jun 4, 2022, 9:47:57 AM6/4/22
to Joomla! General Development
Hi Alex,

Your error message means that $row doesn't hold your table class. It's probably the value "false", indicating that the table class creation didn't work.

I'm just getting to grips with J4 myself so I may be wrong, but I suspect you'll have more luck with

$row = new \\Example\\Component\\Demo\\Site\\Table\\ItemTable

although I've not tested it.

Robin Koehler

unread,
Oct 25, 2023, 2:14:26 PM10/25/23
to Joomla! General Development
I have a similar problem. How can I access the table element from content component?
To be able to delete or add articles programmatically.
The following lines throw errors with an external script:

$mvcFactory = \Joomla\CMS\Factory::getApplication()->bootComponent('com_content')->getMVCFactory();

$articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);

$table = $articleModel->getTable();

$table->load(13);

$table->delete();

Thanks
Robin
Reply all
Reply to author
Forward
0 new messages