Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create A new csv file inside cron job yii2 #231

Closed
omer-sds opened this issue May 28, 2017 · 3 comments
Closed

Create A new csv file inside cron job yii2 #231

omer-sds opened this issue May 28, 2017 · 3 comments

Comments

@omer-sds
Copy link

omer-sds commented May 28, 2017

Issue summary

Cant save / generate a new file to a given path using the following code, i am using it inside my console as a cronjob in Yii2 framework and do not want to output the file instead i want to save it to a path specified if it doesnot exist, my upload_path in params.php file is '/console/uploads/', i tried to check my current working directory when i run it through the console to confirm that it is on my application root i.e. htdocs/Nxb/mess .

System informations

League Csv Version 8.2.0
PHP Version 5.6.21
OS Windows 10 64-bit

Standalone code, or other way to reproduce the problem

Add tho following code inside your console/controllers folder in yii2 advanced-app and run it with
./yii cronjobs/new-subscribers to run it on console

namespace console\controllers;

use Yii;
use yii\console\Controller;
use common\models\Subscriptions;
use League\Csv\Writer;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class CronjobsController extends Controller {

    /**
     * Notifies via email if there were any new subscribers in todays date
     */
    public function actionNewSubscribers() {
        $subscribers = Subscriptions::find ()->where ( ['=' , 'DATE(created_on)' , date ( 'Y-m-d' ) ] )->asArray ()->all ();

        //absolute path for the files 
        $filePath = Yii::$app->params['upload_path'] . 'subscribers.csv';


        //we create the CSV into memory
        $csv = Writer::createFromFileObject ( new \SplFileObject ( $filePath , 'w' ) );

        // The PDOStatement Object implements the Traversable Interface
        // that's why Writer::insertAll can directly insert
        // the data into the CSV
        $csv->insertAll ( $subscribers );

        // Because you are providing the filename you don't have to
        // set the HTTP headers Writer::output can
        // directly set them for you
        // The file is downloadable
        $csv->output ();
    }

Expected result

it should output a file in the given path named subscribers.csv

Actual result

Exception 'RuntimeException' with message 'SplFileObject::__construct(/console/uploads/subscribers.csv): failed to open stream: No such file or directory'

in \xampp\htdocs\Nxb\mess\console\controllers\CronjobsController.php:31

Stack trace:
#0 \xampp\htdocs\Nxb\mess\console\controllers\CronjobsController.php(31): SplFileObject->__construct('/console/upload...', 'w')
#1 [internal function]: console\controllers\CronjobsController->actionNewSubscribers()
#2 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#3 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#4 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\console\Controller.php(128): yii\base\Controller->runAction('new-subscribers', Array)
#5 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\base\Module.php(523): yii\console\Controller->runAction('new-subscribers', Array)
#6 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('cronjobs/new-su...', Array)
#7 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('cronjobs/new-su...', Array)
#8 F:\xampp\htdocs\Nxb\mess\vendor\yiisoft\yii2\base\Application.php(380): yii\console\Application->handleRequest(Object(yii\console\Request))
#9 F:\xampp\htdocs\Nxb\mess\yii(27): yii\base\Application->run()
#10 {main}

@omer-sds
Copy link
Author

sorry had to use absolute path for saving the file used
realPath ( Yii::$app->basePath . '/../' .Yii::$app->params['upload_path'] . '/' ) to fix the issue , closing it now

@omer-sds
Copy link
Author

omer-sds commented May 28, 2017

now it is creating a file but there is no data inside it :( , the new updated code looks like this

$subscribers = Subscriptions::find ()->where ( ['=' , 'DATE(created_on)' , date ( 'Y-m-d' ) ] )->asArray ()->all ();

       //absolute path for the files 
       $filePath = realPath ( Yii::$app->basePath . '/../' .Yii::$app->params['upload_path'] . '/' ) ;
       $fileName   =   $filePath.'/'.'new-subscribers.csv';
       //we create the CSV into memory
       $csv    =   Writer::createFromPath($fileName,'w');

       // The PDOStatement Object implements the Traversable Interface
       // that's why Writer::insertAll can directly insert
       // the data into the CSV
       $csv->insertAll ( $subscribers );

       // Because you are providing the filename you don't have to
       // set the HTTP headers Writer::output can
       // directly set them for you
       // The file is downloadable
       $csv->output ();

@omer-sds omer-sds reopened this May 28, 2017
@omer-sds
Copy link
Author

sorry again had to change the file mode :D to write in the data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant