ExtendedActiveRecord inherits from CActiveRecord, so the basic functions are the same as CActiveRecord
To add support for multiple database connections, a callback to the connectionName() method is added. The usage is consistent with the existing tableName() method and returns the string of the name of the database connection component.
If this method is not defined, the default database connection (db) is used
The source code is as follows:
class ExtendedActiveRecord extends CActiveRecord { public static $db = array(); /** * @return CDbConnection * @throws CDbException */ public function getDbConnection() { $componentName = $this->connectionName(); if (isset(self::$db[$componentName])) { return self::$db[$componentName]; } else { self::$db[$componentName] = Yii::app()->getComponent($componentName); if (self::$db[$componentName] instanceof CDbConnection) return self::$db[$componentName]; else { $message = 'Active Record keyword requires a "' . $componentName . '" CDbConnection application component.'; Yii::log($message, CLogger::LEVEL_ERROR, 'extended'); throw new CDbException(Yii::t('yii', $message)); } } } public function connectionName() { return 'db'; } }
Example:
class SomeModelClass extends ExtendedActiveRecord { ...... public function connectionName() { return 'some-db-connection'; } ...... }
You can connect remotely within the LAN, but cannot connect to the WAN using an external IP address?
The IP address and database port need to be mapped on the router
yii2.0 officially provides two templates
Let’s talk about the basic version first:
Just modify config/db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2',
'username' => 'root',
'password' => ; '',
'charset' => 'utf8',
'tablePrefix' => 'hpcms_',
];
where dbname refers to the database name and host is The host and tablePrefix are the table prefix
which is similar to the advanced version. Modifying the common/config/main-local.php
configuration parameters is basically the same as above!
In this way, you can connect to the database (of course you have to start mysql. If it is another database, please search it. Basically, you can configure the parameters)
For how to operate the database (Add, delete, modify and check) Please see the documentation ActiveRecord and Model (after designing the table structure, you can use Gii to quickly generate the Model)
If you want to know more, it is most practical to read the documentation