I am trying to create a table observer in a module. I am using the Table Advanced API guide in the Joomla documentation https://docs.joomla.org/Table_Advanced_API_Guide. When I use the code provided, the class MyObserver does not work. I have tried making the class abstract, and I have tried extending AbstractObserver instead of implementing JObserverInterface. When I try to make MyObserver abstract, then the createObserver function does not work. When I try to extend AbstractObserver, then it states that it cannot find the class AbstractObserver. How do I get the class MyObserver to work?
I am running Joomla 3.9.22 with PHP 7.0.33. My database is a MySQL version database 10.1.47-MariaDB-0+deb9u1.
myobserver.php
<?php
defined('JPATH_PLATFORM') or die;
class MyObserver implements JObserverInterface
{
// Usually you would define your class as
// class MyObserver extends AbstractObserver
// and then the $table variable and a basic constructor would be included for you
protected $table;
public function __construct($table)
{
$table->attachObserver($this);
$this->table = $table;
$observers = $table->getObserverOfClass("MyObserver");
$observersClass = get_class($observers);
echo "<br>In MyObserver constructor.<br>";
echo "<br>Object returned from getObserverOfClass() has class {$observersClass}<br>";
}
public static function createObserver($table, $params = array())
{
$observer = new self($table);
return $observer;
}
public function onAfterStore(&$result)
{
echo "<br>In observer onAfterStore()<br>";
if ($result)
{
echo "Got table store method notification for table {$this->table->getTableName()}, id {$this->table->id}<br>";
}
}
}
demomodules.php
<?php
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Table\Table;
class TableDemoModules extends Table
{
protected $_jsonEncode = array('params');
function __construct(&$db)
{
parent::__construct('#__modules', 'id', $db);
require 'myobserver.php';
MyObserver::createObserver($this);
}
function check()
{
$this->note .= " added via module";
}
protected function _getAssetName()
{
return "com_modules.module." . $this->id;
}
protected function _getAssetTitle()
{
return $this->title;
}
protected function _getAssetParentId(Table $table = null, $id = null)
{
$assets = self::getInstance('Asset', 'JTable', array('dbo' => $this->getDbo()));
$assets->load(array("name" => "com_modules"));
return $assets->id;
}
}
mod_advanced_table.php
<?php
defined('_JEXEC') or die('Restricted Access');
use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;
$app = Factory::getApplication();
$input = $app->input;
$user = Factory::getUser();
echo "Module id is {$module->id}<br>";
// Tell Joomla to look in the current (module) directory for our table class file
Table::addIncludePath(__DIR__);
$moduleTable = Table::getInstance('DemoModules', 'Table', array());
if ($moduleTable->load($module->id))
{
echo "<br>Module title is {$moduleTable->title}<br>";
// checkout/checkin
if ($moduleTable->isCheckedOut($user->id))
{
echo "<br>module record isCheckedOut call returned true<br>";
}
else
{
echo "<br>module record isCheckedOut call returned false<br>";
}
// ACL rules
if ($rules = $moduleTable->getRules())
{
$rulesString = (string) $rules;
echo "<br>ACL Rules: $rulesString <br>";
}
else
{
echo "<br>getRules() didn't return anything<br>";
}
$userGroups = $user->getAuthorisedGroups();
$randomIndex = array_rand($userGroups);
$newRule = array("core.edit" => array($userGroups[$randomIndex] => 1));
echo "Setting rules to " . json_encode($newRule) . "<br>";
$moduleTable->setRules($newRule);
$moduleTable->store();
// ordering
$where = 'POSITION = "' . $moduleTable->position . '"';
echo "<br>Next order value: " . $moduleTable->getNextOrder($where) . "<br>";
$moduleTable->move(2, $where);
echo "Ordering value is now: {$moduleTable->ordering}<br>";
$where .= " and published = 1";
$moduleTable->reorder($where);
}