Doctrine 在自定义任务中使用原则
我正在symfony 1.4项目中创建一个任务,需要更新一些表 我写过:Doctrine 在自定义任务中使用原则,doctrine,symfony-1.4,Doctrine,Symfony 1.4,我正在symfony 1.4项目中创建一个任务,需要更新一些表 我写过: <?php class dataImportTask extends sfBaseTask { public function configure() { $this->namespace = 'data'; $this->name = 'import'; $this->addOptions(array( new sfCommandOpti
<?php
class dataImportTask extends sfBaseTask
{
public function configure()
{
$this->namespace = 'data';
$this->name = 'import';
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'environment', 'dev'),
));
}
public function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager(sfProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true));
$connection = $databaseManager->getDatabase()->getConnection();
}
}
这是处理任务时的常见问题。您有两种选择:
- 在database.yml中为您的连接命名为
default
- 在database.yml中保持相同的名称,并在每个任务上添加一个选项以检索正确的数据库名称
对于选项2,您必须添加一个带有数据库名称(在数据库.yml
中定义的名称)的选项,然后更改getDatabase
的工作方式:
在database.yml中:
all:
doctrine:
class: sfDoctrineDatabase
根据上述条令名称,在任务中添加选项:
<?php
class dataImportTask extends sfBaseTask
{
public function configure()
{
$this->namespace = 'data';
$this->name = 'import';
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
));
}
public function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager(sfProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true));
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
}
}
这是处理任务时的常见问题。您有两种选择:
- 在database.yml中为您的连接命名为
default
- 在database.yml中保持相同的名称,并在每个任务上添加一个选项以检索正确的数据库名称
对于选项2,您必须添加一个带有数据库名称(在数据库.yml
中定义的名称)的选项,然后更改getDatabase
的工作方式:
在database.yml中:
all:
doctrine:
class: sfDoctrineDatabase
根据上述条令名称,在任务中添加选项:
<?php
class dataImportTask extends sfBaseTask
{
public function configure()
{
$this->namespace = 'data';
$this->name = 'import';
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
));
}
public function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager(sfProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true));
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
}
}