Is possible to share a DB connection between web app and console app?

As we know, PHP web application (also Yii2) establish a DB connection when application start and shut it down with application end. So we can’t share a DB connection between web app and console app directly. For example we’d like to accept a web request and generate some temporal tables via the background console app (proc_open), then access them in this web request subsequent process.

So, Is there a mechanism to achieve it?

Any suggestion is appreciated, thank you.

You could try from web action to launch console command:




public function actionTest()

{

	$phpFile = \Yii::$app->basePath.'/yii command-to-execute';

	$cmd = sprintf("php %s", $phpFile);

	shell_exec(sprintf('%s > /dev/null 2>/dev/null &', $cmd));

}



Hi, thank you Fabrizio!

We had tried to make a console call via yii2-command-bus --> symphony-process --> proc_open, and it failed. It can’t see the temporary tables which created by web in the console process. Theoretically speaking, three are two separated PROCESSES, and they maintenance DB connections themselves. I think shell_exec should leads the same result…

Datatabase temporary table are destroyed when client script execution finish.

So you can’t access temporary table created in an external script.