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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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:

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


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();
}

Lawrence Krubner | 03/02/11 at 12:27am Edit
Tutorial: How to assign prize money


(3) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/02/11
    3:08pm
    Gabor Fasi says:

    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`.

    Previous versions of this answer: 03/02/11 at 3:08pm

  • avatar
    Last edited:
    03/03/11
    5:24am
    Loban Rahman says:

    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;
    }

  • avatar
    Last edited:
    03/02/11
    12:37pm
    Lawrence Krubner says:

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

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.