The last time we implemented the driver class for the model, because ConnectionManager needed to instantiate the driver class according to the driver name configured in the configuration file, we first added the driver name to the configuration file:
03 |
' Defaultcontroller ' => ' Index ', |
04 |
' DefaultAction ' => ' index ', |
06 |
' Errorreporting ' =>-1, |
07 |
' TimeZone ' => ' PRC ', |
09 |
' DSN ' => ' Mysql:dbname=test;host=localhost ', |
The driver underneath the DB is the driver name.
After the configuration file is ready, ConnectionManager two static methods for this class: Getconnection and Releaseconnection implementations are simple, First getconnection is to select a driver class according to the configuration in the configuration file, then instantiate, of course, since the model file may have more than one, then the database connection is best, so this place must be a single case, releaseconnection is simpler, You can call the Close method of the driver class directly.
02 |
Class ConnectionManager extends Base { |
03 |
private static $_instance = null; |
04 |
public static function getconnection () { |
05 |
if (self::$_instance = = null) { |
06 |
Follow the configuration file to find the driver class and then connect to DB |
07 |
Switch (C (' Db=>driver ')) { |
09 |
Self::$_instance = new Pdodriver (); |
12 |
Self::$_instance = new Pdodriver (); |
16 |
return self::$_instance; |
18 |
public static function Releaseconnection () { |
19 |
if (null!== self::$_instance) { |
20 |
Self::$_instance.close (); |
After ConnectionManager, we'll talk about Modelbase, because this class is the parent of the table model and the relational model, so it basically defines all the interfaces that are externally accessible, such as Execute,select,update,insert, Delete, and so on. Now let's write a simpler modelbase, in this case only the constructor, Execute,getall three methods, execute is to execute a sql,getall is to get the data.
Note: If we can return the data according to the object, the normal array or the iterator, then the getall is more complicated here, but now we call Getallbyassocarray directly in GetAll, so this method is very simple.
OK, words do not say more, directly paste code!!
02 |
Class Modelbase extends Base { |
03 |
protected $_db = null; |
04 |
Public Function __construct () { |
05 |
$this->_db = Connectionmanager::getconnection (); |
07 |
Public function Execute ($sql, Array $arr) { |
08 |
$this->_db->prepare ($sql); |
09 |
$this->_db->execute ($arr); |
11 |
Public Function GetAll () { |
12 |
return $this->_db->getallbyassocarray (); |
Since there are not a few lines of code here, so I do not speak, the following is a little more complex ah ...
I had a separate SQL Parse in the B, which basically completes the parsing of SQL, and we know a simple query statement format as follows:
SELECT [alldistinct] Field from Table WHERE wherecondition [GROUP by groupcondition] [have havingcondition] [order BY Ordercondition]
Before we were all writing a SQL string directly, this way is not conducive to our expansion, if we only need to specify a part of the content of SQL, the other content framework can be automatically completed, this maintenance easier, of course, complex SQL I do not recommend this.
For example, in the table model, we specify the table name, then the framework should be able to identify the tables together, and field this part can default to *,wherecondition if there is no can not write, all or distinct default is all.
Now assume that in a table model, the associated table is user, then execute a $this->select (), the original point of view the system does not know what the query statement is, naturally can not execute, but if the framework can automatically complete SQL, this SQL becomes:
SELECT * from user
There is no problem with such an SQL statement.