问题
How do I check if the initialisation was successful after I do this?
ActiveRecord\Config::initialize(function($cfg){
$cfg->set_model_directory('models');
$cfg->set_connections(array(
'development' => "mysql://root:root@localhost/blog"
));
});
And then I go onto doing something like:
$posts = Post::all();
Just before performing the above I'd like to check if the initialization was successful.
With a PDO object I could do:
if $con ? /*execute query*/ : /*failed to connect*/
How could I achieve the same assurance with ActiveRecord?
回答1:
You can either check the connection from some instance you have (lets say you have got some model instantiated as $model
), like so:
$model->connection();
You can probably do it statically, by calling the connection class
\Activerecord\Connection::instance();
来源:https://stackoverflow.com/questions/17637739/php-activerecord-how-to-check-if-connection-to-database-is-successful