This command controllers are used for scheduler tasks.
The major use of commandController is, we can get the extensions/plugin settings for manipulations. But in actual scheduler tasks, this is difficult.
In below command controller, I am showing how to update the status of the custom database table on every day automatically using Schedular Task.
Here I am getting the record Id to update, from settings.
1. Create the following file
myExt/Classes/Command/StatusUpdateCommandController.php
2. Write the below code in that file
<?php
namespace Vendor\Myext\Command;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class StatusUpdateCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController {
/**
* @var TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
* @inject
*/
protected $configurationManager;
/**
* The settings.
* @var array
*/
protected $settings = array();
/**
* Initialize the controller.
*/
protected function initializeCommand() {
// get settings
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
‘MyextName’, ‘mypluginName’
);
}
/**
* Main function
*@return void
*/
public function StatusUpdateCommand() {
// settings
$this->initializeCommand();
$mystatus = 1;
$this->statusUpdate($mystatus, $this->settings[‘updateRecordId’]);
return true;
}
/*
* Update status
*/
private function statusUpdate($mystatus,$recordUid) {
$statusUpdateArray = array(
‘mystatus’ => $mystatus,
‘tstamp’ => $GLOBALS[‘EXEC_TIME’]
);
$GLOBALS[‘TYPO3_DB’]->exec_UPDATEquery(‘tx_myext_domain_model_mytable’,’uid=’.$recordUid, $statusUpdateArray);
}
}
?>
3. In setup file “Myext\Configuration\TypoScript\setup.txt”
plugin.tx_myext {
settings {
updateRecordId = 1
}
}
module.tx_myext < plugin.tx_myext
4. Register this command controller in local_conf.php of the extension.
$GLOBALS[‘TYPO3_CONF_VARS’][‘SC_OPTIONS’][‘extbase’][‘commandControllers’][] = ‘Vendor\Myext\Command\StatusUpdateCommandController’;
5. Now we can get this command controller task under the “extbase / Extbase CommandController Task” menu entry of “Scheduler task”.
That’s it.