I am developing an Yii2 application and at times, the Sqlite3 file which I use for database, becomes "read only" (due to other stuff happening on the server) and users are getting nasty PHP exception errors instead of a nicely displayed error message. I am trying to use the "beforeSave()" function to check if the databse is writable, but I don't know the best way to check if the web process (www-data) has write access to it. Anyone any ideas? I have the following two functions in my base model:
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
// Check if database is writable
if($this->attemptDBWrite())
return true;
else
{
\Yii::$app->getSession()->setFlash('error', 'Database is not writable by the application user');
return false;
}
} else {
return false;
}
}
And the "attemptDBWrite()" function:
public function attemptDBWrite()
{
try {
$connection = \Yii::$app->db;
// SOME CODE NEEDED HERE TO CHECK <<<=========
} catch (\yii\db\Exception $e) {
\Yii::$app->getSession()->setFlash('error', var_dump($e));
return false;
}
}