


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
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

The latest Mi su7 model car launched by Xiaomi has dominated various hot search lists. Many users who happen to want to buy a car have chosen Xiaomi su7 model car for purchase. So how do you use your Xiaomi car app to bind the car after picking up the car? If you decide to use a home charging pile for charging, this tutorial guide will give you a detailed introduction, I hope it can help you. First, we open the Xiaomi mobile app, click the My button in the lower right corner, and then in the My interface, you can see the option of home charging pile. After entering the page to bind the charging pile, click the scan code button below and scan the QR code on the charging pile. The QR code can be used to bind the charging pile to the app.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original
