Doctrine ODM Calling ->toArray on a Cursor causes CLI script called by exec to do a void return (only on exec/shell_exec call)

StackOverflow https://stackoverflow.com/questions/16786763

Question

I use a CLI script to do some stuff. Today i want to adapte it to show me results on my browser rather than on temrinal. It works everytime perfectly when called called from terminal but not when i call it from exec php command WHEN there is a call on the doctrine Cursor::toArray. See bellow :

it executes the following lines of code :

        $parses=$dm->createQueryBuilder("App\Document\Parse")
                ->field('positions.website')->equals($name)
                ->getQuery()->execute();
        /* @var $parses Doctrine\ODM\MongoDB\Cursor */

        $details=[];




        foreach($parses as $parse){

            /* some other things */
        }


        echo json_encode($details);

when called form terminal (php cli.php find parses -site test.com) it return me a nice json document.

But now I want to show it on a browser. Then i do a little script (http accessable) calling 'exec("php cli.php find parses -site test.com")' . And it returns me nothing. (also tryed shell_exec ; also tryed with other scripts : they work)

Now i delete the foreach loop. I got the following code :

        $parses=$dm->createQueryBuilder("App\Document\Parse")
                ->field('positions.website')->equals($name)
                ->getQuery()->execute();
        /* @var $parses Doctrine\ODM\MongoDB\Cursor */

        $details=[];

        echo json_encode($details);

When i call it from exec : it returns me a nice empty json string

(Remember that each case still works when i call it directly from terminal)

Now i try to call ->toArray() on the doctrine cursor :

        $parses=$dm->createQueryBuilder("App\Document\Parse")
                ->field('positions.website')->equals($name)
                ->getQuery()->execute();
        /* @var $parses Doctrine\ODM\MongoDB\Cursor */

        $details=[];

        $parses->toArray();

        echo json_encode($details);

It returns nothing when i call it from exec or shell exec, but it returns a well "array(0) {}" when called from terminal itself.

It tryed other script with exec and shell exec, they all work and do good return, only when they dont use Doctrine cursor toArray

Was it helpful?

Solution

When you run a PHP script from the terminal, it runs with the permission of the running user. When you run a PHP script on a web server, it runs with the permissions of the server process.

Check that the user with which you are running the script has permissions to access the same files in the web directory as the user that the server is running as.

Also, have you turned on error_reporting and checked the error_log files?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top