ThinkPHP3.1 new features For more complete database operation _php Tutorial

Source: Internet
Author: User
Typically, if an application simply operates on the same database (or distributed database), you only need to define the database connection information in the project configuration file. The multi-database operation described here generally refers to the operation of a different database in one application (including the same type and different types of databases), even the case of dynamic switching of multiple databases.

For earlier versions of thinkphp, switching databases requires an advanced model, and now the 3.1 version is easier to solve.

The new version of multi-data support in the following ways, developers can choose the right way to operate according to the actual situation:

1. Model Definition Database

If it is simply a cross-library operation and is only an individual model class, you can define the dbname attribute directly in the model class:

protected $dbName = ' top ';

When instantiating, remember to use the D method, for example:

$User = D (' User ');

This is defined as the premise that the current database user account has permissions to manipulate the top database.

2. Model Definition Database connection

If your cross-library operation requires a different database connection account or you need to connect to a different type of database, you can define the connection property directly within the model class, and automatically connect to the specified database when you manipulate the model class. For example:

protected $connection = ' mysql://root:1234@localhost:3306/thinkphp ';

Or use an array method to define:

Protected $connection = Array (  ' db_type ' = ' mysql ',  ' db_user ' = ' root ',  ' db_pwd ' = '  1234 ',  ' db_host ' = ' localhost ',  ' db_port ' = ' 3306 ',  ' db_name ' = ' thinkphp ');

If we have configured additional database connection information in the configuration file, for example:

  Database configuration 1  ' db_config1 ' = Array (    ' db_type ' = ' mysql ',    ' db_user ' = ' root ',    ' db_pwd '  = > ' 1234 ',    ' db_host ' = ' localhost ',    ' db_port ' and ' 3306 ',    ' db_name  ' and ' thinkphp ',  //Database Configuration 2  ' db_config2 ' = ' mysql://root:1234@localhost:3306/thinkphp ';

So, we can change the property definition of the model class to:

Call the database configuration file in configuration 1 protected $connection = ' db_config1 ';

Or:

Call the database configuration file in configuration 2 protected $connection = ' db_config2 ';

The advantages of this approach can support different database types, that is, it can be different from the database type in the current project configuration file, the disadvantage is that the D method must be instantiated and cannot be set dynamically.

3. Model instantiation of a specified connection

The new version supports specifying a database connection when instantiating a model, for example:

or instantiate using the M method:

The second parameter of the M method is the prefix of the data table, and if left blank indicates a data table prefix with the project configuration, the third parameter is the database connection information required for the current instantiation.

Similarly, the database connection information that is passed in the instantiation can also be configured in the form of a name, for example:

If the current operation does not need to switch the database connection, just need to switch the database, you can use:

Represents the Think_user data table that instantiates the top database. If your data table does not have a prefix, you can use the

Represents the user table that instantiates the top database.

4. Dynamic Switch Connection

The system also provides a more flexible dynamic operation, using the DB method provided by the model class for multi-database connection and switching operations, usage:

MODEL->DB ("Database Number", "Database Configuration");

Database number format, for the database connection has been called, is no need to re-incoming database connection information, the system will be automatically recorded. For the initialized database connection, the internal database number is 0, so in order to avoid conflicts, do not define the database configuration with database number 0 again.

The database configuration is defined in the same way as the model definition connection property, which supports arrays, strings, and the invocation of configuration parameters in three formats.

The DB method call returns the current model instance and can proceed to other operations of the model directly, so the method can be dynamically toggled during the query, for example:

$this->db (1, "Mysql://root:123456@localhost:3306/test")->query ("Query SQL");

This method adds a database connection numbered 1 and automatically switches to the current database connection.

When you switch to the same database for the second time, you do not need to pass in the database connection information, you can use it directly:

$this->db (1)->query ("Query SQL");

All current operations are the database set for DB (1) before the database switchover is done again.
If you need to switch to the default database connection, you only need to call:

$this->db (0);

If we have defined additional database connection information in the project configuration, we can call the configuration directly in the DB method to connect:

$this->db (1, "DB_CONFIG1")->query ("Query SQL"), $this->db (2, "Db_config2")->query ("Query SQL");

If after switching the database, the data table and the current inconsistency, you can use the table method to specify the data table to manipulate:

$this->db (1)->table ("Top_user")->find ();

If you want to return the current database connection, you can call the empty db method directly, for example:

$db = $this->db ();

http://www.bkjia.com/PHPjc/825453.html www.bkjia.com true http://www.bkjia.com/PHPjc/825453.html techarticle Typically, if an application simply operates on the same database (or distributed database), you only need to define the database connection information in the project configuration file. Here ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.