Home > PHP Framework > YII > How to connect Yii2 to PostgreSql

How to connect Yii2 to PostgreSql

angryTom
Release: 2020-02-02 21:12:47
forward
3189 people have browsed it

This article introduces the method of connecting the Yii2 framework to PostgreSql. It has certain reference value. Now I will share it with you. I hope it will be helpful to you!

How to connect Yii2 to PostgreSql

1. Configure the connection information to connect to postgresql:

$dev = [
    'class'    => 'yii\db\Connection',
    'dsn'      => 'pgsql:host=122.112.182.211;port=8000;dbname=sdk_info',
    'username' => 'dbadmin',
    'password' => 'Styl2018@',
    'charset'  => 'utf8',
//    'emulatePrepare' => true
];
return $dev;
Copy after login

2. In the Yii2 directory, create a new one in the models directory. model:

class TestSdkVsent extends Base
{
    public static function getDb()
    {
        return \Yii::$app->dbDws;
    }
    public static function tableName()
    {
        return 'sdk_info.tb_sdk_vsent'; // TODO: Change the autogenerated stub
    }
    public static function test()
    {
        $find = static::find();
        $result = $find->select('rtt')->limit(1) ->asArray()->one();
        return $result;
    }
}
Copy after login

3. Remarks:

$result = $find->select('rtt')->limit(1)->asArray()->one(); 查询到一条数据返回
Copy after login

Analysis result: ``SELECT * FROM "sdk_info"."tb_sdk_vsent" LIMIT 1

$result = $find->select('rtt')->asArray()->one(); 会查询所有的数据返回一条
Copy after login

Parsing results: SELECT "rtt" FROM "sdk_info"."tb_sdk_vsent"

Recommended related articles and tutorials: yii tutorial

The above is the detailed content of How to connect Yii2 to PostgreSql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
yii2 error connecting to mongodb3.2.4
From 1970-01-01 08:00:00
0
0
0
How to use mongodb to do rbac in yii2
From 1970-01-01 08:00:00
0
0
0
php - yii2-ueditor-widget
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template