Multiple design modes are used in ZendFramework. The database encapsulation uses the factory model. First, we will introduce some concepts:
Factory mode: defines a class to create instances of other classes. the created instance usually has its own parent class. The factory mode belongs to the class creation mode. instances of different classes are usually returned based on different independent variables.
The essence of the factory model is that a factory class dynamically determines the product instance to be created based on the input parameters. Factory models involve factory roles, abstract product roles, and specific product roles.
The Creator role is the core of the factory model. it is responsible for creating internal slaves for all instances. The factory class can be directly called by the outside world to create the required product objects.
Abstract Product role: the parent class of all objects created in Factory mode. it describes the common interfaces of all instances.
The role of a specific Product is the creation target of the factory model. all objects are instances of a specific class that acts as the role.
Zend_db in ZF is a good example of the factory model.
Next, we will start the analysis ......
When configuring zf, we can store the database connection operation information in the Bootstrap. php file.
Copy codeThe code is as follows:
Class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
Function _ construct ($ app ){
Parent: :__ construct ($ app );
$ Url = constant ('application _ path'). DIRECTORY_SEPARATOR. 'configs'. DIRECTORY_SEPARATOR. 'config. ini ';
$ Dbconfig = new Zend_Config_Ini ($ url, null, true );
$ Db = Zend_Db: factory ($ dbconfig-> general-> db-> adapter, $ dbconfig-> general-> db-> params-> toArray ());
// Var_dump ($ db );
$ Db-> query ('set NAMES utf8 ');
Zend_Db_Table: setdefaadapter adapter ($ db );
}
}
?>
In the entry file, use a Zend_Application object to call bootstrap (), and the Bootstrap-like constructor will be called.
In the constructor, we can get an object instance that operates the database through Zend_Db: factory.
Read the information in config. ini from a Zend_Config_Ini instance and pass it as a parameter to the factory function Zend_Db: factory ()
Config. ini information
[General]
Db. adapter = PDO_MYSQL
Db. params. host = localhost
Db. params. username = root
Db. params. password =
Db. params. dbname = database name
Zend_Db: factory ()
Parameter 1 indicates the database type to be operated, such as PDO_MYSQL
Parameter 2: indicates the information of the database to be connected, including the server name, user name, password, and database to be connected.
First, two questions are thrown:
① If the database we want to operate is MSSQL, what should we do?
② Here we use Zend_Db: factory (). if we use the traditional method, how can we operate it?
Answer:
① You only need to modify PDO_MYSQL to PDO_MSSQL in the config. ini file.
② Create an object instance for database operations in a traditional way:
$ Db = new Zend_Db_Adapter_Pdo_Mysql ($ config)
Where: $ config information is read from config. ini
Question: If we use the traditional method to create an object instance, we must have a process to determine the database type to be operated currently?
For example:
Copy codeThe code is as follows:
Switch ($ dbType ){
Case 'pdo _ mysql ':
....
Case 'pdo _ mssql ':
....
Case 'pdo _ SQLITE ':
....
}
We have to write different statements to operate the database based on different database types, which is not very troublesome.
However, zf has helped us through the factory model and is very convenient to use.
In Zf, how does one implement the factory model?
First, we need to have an abstract base class: Zend_Db_Adapter_Abstract, which is the parent class of all objects created in the factory mode. it is responsible for providing interfaces to be shared by all instances.
This class not only provides some implementation methods that we are very familiar with database operations, such as select, update, insert, delete, query, fetchRow, and fetchAssoc, but also provides some interfaces, it is implemented in sub-classes, such as limit, getServerVersion, closeConnection, and describeTable.
Copy codeThe code is as follows:
Abstract class Zend_Db_Adapter_Abstract
{
//..
}
Abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
{
//..
}
Class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract
{
//... Perform operations on the Mysql database
}
Class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
{
//... Implement operations on the Mssql database
}
Class Zend_Db_Adapter_Pdo_Sqlite extends Zend_Db_Adapter_Pdo_Abstract
{
//... Implement operations on the Sqlite database
}
The preceding relationship can be expressed in a simple figure.
Next, we will track whether Zend_Db: Factory () is used to select different databases based on different parameters.