Home > Backend Development > PHP Tutorial > Yii ExtendedActiveRecord enhanced version of ActiveRecord adds multi-database connection binding function, activerecord_PHP tutorial

Yii ExtendedActiveRecord enhanced version of ActiveRecord adds multi-database connection binding function, activerecord_PHP tutorial

WBOY
Release: 2016-07-13 10:21:11
Original
936 people have browsed it

Yii ExtendedActiveRecord enhanced version of ActiveRecord adds multi-database connection binding function, activerecord

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';
    }
}
Copy after login

Example:

class SomeModelClass extends ExtendedActiveRecord
{
    ......

    public function connectionName() {
        return 'some-db-connection';
    }

    ......
}
Copy after login

 

Ralis connects to the remote database and prompts ActiveRecord::ConnectionNotEstablished. Local pl/sql can access the remote database

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

How to connect yii20 to XAMPP database?

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/860062.htmlTechArticleYii ExtendedActiveRecord enhanced version of ActiveRecord adds multi-database connection binding function, activerecord ExtendedActiveRecord inherits from CActiveRecord, so the basic functions are the same as CActiveReco ...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template