Is there any way to catch Database_Exception [ 1146 ] (Table 'table_name' doesn't exist) and create one (CREATE TABLE IF NOT EXISTS), according to table structure listed in model ($_table_columns)?
Any assistance would be appreciated.
UPDATED 29.07.2015 -----------------------------------------------
I solved the exeption cathing part of my question following way:
class Database_Exception extends Kohana_Database_Exception {
/**
* Creates a new translated exception.
*
* throw new Kohana_Exception('Something went terrible wrong, :user',
* array(':user' => $user));
*
* @param string $message error message
* @param array $variables translation variables
* @param integer|string $code the exception code
* @param Exception $previous Previous exception
* @return void
*/
public function __construct($message = "", array $variables = NULL, $code = 0, Exception $previous = NULL)
{
// Search for corresponding handler class
if(class_exists('Database_Exception_Handler_' . $code))
{
// Pass the $variables array to the handler
$handler_class_name = 'Database_Exception_Handler_' . $code;
$handler_class_name::instance($variables)->handle();
// Abort further script execution
exit(1);
}
else
{
// Pass the message and integer code to the parent
parent::__construct($message, $variables, $code, $previous);
}
}
}
class Database_Exception_Handler_1146 {
/**
* Class instance.
*
* @static
* @access protected
* @var object
*/
protected static $_instance;
/**
* Exeption variables.
*
* @static
* @access protected
* @var array
*/
protected static $_variables;
/**
* Creates a singleton Database Exception
* Handler instance.
*
* @static
* @access public
*
* @param array $variables
* @return object
*/
public static function instance(array $variables = NULL)
{
if( ! is_object(self::$_instance))
{
$handler_class_name = __CLASS__;
self::$_variables = $variables;
self::$_instance = new $handler_class_name();
}
return self::$_instance;
}
public static function handle()
{
// Here comes handler code...
}
} // End Database_Exception_Handler_1146
So now we can catch and handle any database table errors. In example - [1146] - Table 'database_name.table_name' doesn't exist.
Now I'm wondering how to create table (// Here comes handler code...), and check table structure, according to model property $_table_columns.
Any idea?