Home > Tags > bat
bat
symfony1.2でバッチ処理を作るにはどうすればいいの?
- 2009/01/14
- Web開発関連
symfony1.2 では generate:task コマンドが用意されているのでそれを利用します。
- まず、ジェネレートコマンドでひな形を作る
- execute 関数をカスタマイズする
- バッチ処理を実行する
- 引数を使いたいとき
- 他にも
ジェネレートコマンドを実行すると、
fooSampleTask.class.php というファイルが作成される
<?php
class fooSampleTask extends sfBaseTask
{
protected function configure()
{
// // add your own arguments here
// $this->addArguments(array(
// new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
// ));
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
// add your own options here
));
$this->namespace = 'foo';
$this->name = 'sample';
$this->briefDescription = '';
$this->detailedDescription = <<<EOF
The [foo:sample|INFO] task does things.
Call it with:
[php symfony foo:sample|INFO]
EOF;
}
protected function execute($arguments = array(), $options = array())
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
// add your code here
}
}
『add your code here』の下に実際の処理を記述していきます。
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
// DB アクセスも普通に使える
$obj = BookPeer::retrieveByPK(3);
var_dump($obj);
// ログも便利
$this->log('処理終了');
}
{
// // add your own arguments here
$this->addArguments(array(
new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
));
configure 関数の先頭のコメントを外すと my_arg 引数が有効になり、execute 関数の引数 $arguments['my_arg'] で利用できるようになります。
symfony/task/project とか
symfony/task/generator とか
symfony のライブラリー内のタスクにサンプルになりそうなファイルがあるので参考にしてください。
ホーム > タグ > bat