Step-by-step writing of PHP's Framework (18)

Source: Internet
Author: User
Tags array query return table name

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:

01 <?php
02 Return Array (
03 ' Defaultcontroller ' => ' Index ',
04 ' DefaultAction ' => ' index ',
05 ' Debug ' => true,
06 ' Errorreporting ' =>-1,
07 ' TimeZone ' => ' PRC ',
08 ' DB ' => array (
09 ' DSN ' => ' Mysql:dbname=test;host=localhost ',
10 ' User ' => ' test ',
11 ' pwd ' => ' test ',
12 ' Driver ' => ' PDO '
13 )
14 );

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.

01 <?php
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 ')) {
08 Case ' PDO ':
09 Self::$_instance = new Pdodriver ();
10 Break
11 Default:
12 Self::$_instance = new Pdodriver ();
13 Break
14 }
15 }
16 return self::$_instance;
17 }
18 public static function Releaseconnection () {
19 if (null!== self::$_instance) {
20 Self::$_instance.close ();
21st }
22 }
23 }

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

01 <?php
02 Class Modelbase extends Base {
03 protected $_db = null;
04 Public Function __construct () {
05 $this->_db = Connectionmanager::getconnection ();
06 }
07 Public function Execute ($sql, Array $arr) {
08 $this->_db->prepare ($sql);
09 $this->_db->execute ($arr);
10 }
11 Public Function GetAll () {
12 return $this->_db->getallbyassocarray ();
13 }
14 }

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.



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.