PHP ActiveRecord如何检查与数据库的连接是否成功? [英] PHP ActiveRecord how to check if connection to database is successful?
本文介绍了PHP ActiveRecord如何检查与数据库的连接是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
执行此操作后如何检查初始化是否成功?
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.
使用 PDO对象,我可以这样做:
if $con ? /*execute query*/ : /*failed to connect*/
如何通过ActiveRecord获得相同的保证?
How could I achieve the same assurance with ActiveRecord?
推荐答案
您可以从您拥有的某个实例中检查连接(例如,您已将某些模型实例化为$model
),如下所示:
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();
这篇关于PHP ActiveRecord如何检查与数据库的连接是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文