I have made progress in my story and would like to present my experiences. What I have to say is probably obvious to you and many other readers, however for those of us on a learning curve simple examples are very useful.
My initial problem was that I had not prepared my console bootstrap file correctly. What I am now using is as follows:
<?php
/**
* Yii console bootstrap file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$composerAutoload = [
__DIR__ . '/../vendor/autoload.php', // in yii2-dev repo
__DIR__ . '/../../autoload.php', // installed as a composer binary
];
$config = require __DIR__ . '/config/console.php';
$vendorPath = null;
foreach ($composerAutoload as $autoload) {
if (file_exists($autoload)) {
require $autoload;
$vendorPath = dirname($autoload);
break;
}
}
$application = new yii\console\Application($config) ;
if ($vendorPath !== null) {
$application->setVendorPath($vendorPath);
}
$exitCode = $application->run();
exit($exitCode);
?>
The documentation is correct, however it could be a lot clearer/simper in many respects. Like a lot of technical documentation, it provides answers easily found by those who already know the answers, but for those of us finding our way around the yii framework it is less than intuitive I would suggest.
The object represented by the string Yii::$app->db is an instantiaton of the Yii\db\Connection class, fully populated, and ready for use.
If I wish to continue using the yii\db\Connection class together with the yii\db\Command and related classes, then I simply open the connection as follows:
Yii::$app->db->open()
or, with some PDO related attributes defined
Yii::$app->db->open([
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
]);
and off I go. If, however, I wish to use the PDO related classes directly, I can extract the necessary connection values as follows:
$dbdsn = Yii::$app->db->dsn;
$dbuser = Yii::$app->db->username;
$dbpass = Yii::$app->db->password;
These are the values defined in the db.php file conventionally located in the config directory, hence achieving my goal of leveraging the meta data defined in the yii framework configuration.
These same values can be used to support usage of any of the available Database Access Objects, such as the mysqli API set.
In my Windows world, I invoke my console application with the following CLI command executed from my application root directory:
php yii.php bam/command Testing ProcessTesting
“bam” is the name of the command controller to be used. This file, named BamController.php is located in the commands subdirectory of my application root directory. “command” is the action to be invoked within the controller module. An empty copy of my BamController.php is as follows:
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\commands;
use yii\console\Controller;
use yii\console\ExitCode;
class BamController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
* @return int Exit code
*/
public function actionCommand($batchCommand, $batchFunction)
{
… application specific code here
return ExitCode::OK;
}
}
The values “Testing” and “ProcessTesting” are assigned to the variables $batchCommand and $batchFunction in the function actionCommand.
Hopefully this is useful for others.