Ask your Symfony questions! Pay money and get answers fast! (more info)

How do I call getHost() when I'm in a task? Symfony

I'm using Symfony 1.4.

I have the following code. Do you see these 2 lines:


$webRequest = new sfWebRequest();
$host = $webRequest->getHost();

I get this error:

<blockquote>Catchable fatal error: Argument 1 passed to sfRequest::__construct() must be an instance of sfEventDispatcher, none given, called in /home/lkrubner/dev/tastingnotes/lib/task/process_sql_for_searches_to_render_an_export_fileTask.class.php on line 77 and defined in /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/request/sfRequest.class.php on line 44</blockquote>

What do I need to do to find the host, while I'm in a task?



protected function execute($arguments = array(), $options = array())
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
$rawSqlConnection = Doctrine_Manager::connection();

$q = Doctrine_Query::create()
->select('s.*')
->from('SearchesForExportToBeRunByCronScript s')
->where("s.has_this_search_been_run = 'f'");
$searches = $q->execute();

foreach ($searches as $search) {
$userWhoCreatedThisSearch = Doctrine_Core::getTable('sfGuardUser')->find($search['user_id']);
$searchExportFileName = $userWhoCreatedThisSearch->getUsername() . time() . '.csv';
$pathToFile = sfConfig::get('sf_web_dir') . '/uploads/search_exports/' . $searchExportFileName;
$arrayOfIdsToFetch = unserialize($search['sql_for_search']);

$filePointer = fopen( $pathToFile, 'w');

for ($i=0; $i < count($arrayOfIdsToFetch); $i++) {
$row = $arrayOfIdsToFetch[$i];
$idToFetch = $row['id'];
$query = "SELECT * FROM wine WHERE id=$idToFetch";
$statement = $rawSqlConnection->execute($query);
$statement->execute();
$resultset = $statement->fetch(PDO::FETCH_OBJ);
$dataArr = array();

foreach ($resultset as $property) {
$dataArr[] = $property;
}

unset($resultset); //Save memory
fputcsv($filePointer, $dataArr);
unset($dataArr);
}

fclose($filePointer);

$webRequest = new sfWebRequest();
$host = $webRequest->getHost();
$absolutePath = $host . '/uploads/search_exports/' . $searchExportFileName;
$message = "
Your search results are available here:

$absolutePath
";
$message = wordwrap($message, 70);

mail($userWhoCreatedThisSearch->getEmailAddress(), 'Your search results are ready for download', $message);

echo $userWhoCreatedThisSearch->getEmailAddress(), 'Your search results are ready for download', $message;

$search['has_this_search_been_run'] = 't';
$search->save();
}

Answers (2)

2011-03-02

Gabor Fasi answers:

The host value comes from the Host http header, and you are not in http context while running a task - thus not being able to determine the site's host.

Your best option is to include it in your app.yml, and use that value in the task.

Edit: you can pass an application like `--application=frontend`, and symfony will load the application's settings. You can also specify an environment like `--env=prod`.


Lawrence Krubner comments:

When I'm in a task I am not in an app, so I do not see how app.yml could be of use.

2011-03-03

Loban Rahman answers:

I'm very confused how you except this to work. sfWebRequest objects are created when symfony receives a web request, and the host comes from the web server (e.g. apache). Apache can create any number of virtual hosts and each one will pass a different host to symfony.

This is a task run using the cli version of php. There is no web request. The best you can do is get back the hostname of the machine, but this is almost certainly not match the web host that you want in order to create the absolute URL. You need to either save this in the database or, as Gabor suggested, in an app.yml file.

And you are definitely in an app when running a task. Check the configure method of your task class. I pasted the contents from a task from a project of mine.


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', 'front'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
// add your own options here
));

$this->namespace = 'dance';
$this->name = 'receive-email';
$this->briefDescription = 'Parses email for commands';
$this->detailedDescription = <<<EOF
The [dance:receive-email|INFO] task parses an email piped into it's standard input for
commands relating to reserving, cancelling, and waiting for classes. Postfix should be
configured to pipe incoming email to this task.
Call it with:

[php symfony dance:receive-email|INFO]
EOF;
}