ThinkPHP connection to the Oracle database (2)

Source: Internet
Author: User
Recently I collected some questions about how THinkPHP connects to the Oracle database. Many of my friends follow the mysql connection method, which causes some methods to be unavailable in Oreale. For example, the findAll and Select methods cannot be used to obtain the required data. The Create and add methods cannot Create or write data to the database.

Recently I collected some questions about how THinkPHP connects to the Oracle database. Many of my friends follow the mysql connection method, which causes some methods to be unavailable in Oreale. For example, the findAll and Select methods cannot be used to obtain the required data. The Create and add methods cannot Create or write data to the database.

Recently I collected some questions about how THinkPHP connects to the Oracle database. Many of my friends follow the mysql connection method, which causes some methods to be unavailable in Oreale. For example, the findAll and Select methods cannot be used to obtain the required data. The Create and add methods cannot Create or write data to the database.



As a matter of fact, after debugging for several days, I found the problem and successfully used it properly in my own small project exercises. Now I will share my experience with you.

1. I will not talk about the connection and configuration file content. The above has been explained. Here I will only describe my operations based on an example of a data table.

2. The table structure is as follows:


3. This table has three fields: ID primary key, username, and password pass. Because oracle converts both the table name and field to uppercase, auto-increment of ID primary key is not supported, I only need to use another method to implement this function, for example: ID auto-sequence + trigger to implement ID auto-increment.

4. In ThinkPHP, It is a controller, a Model, and a view is embodied in a template.

First, let's talk about the controller. I only want to introduce how to add and retrieve lists.

Next, let's talk about the model. Here is the main reason for success. Why? ThinkPHP supports field ing, which is perfect for MYSQL. You don't need to write a MODEL, but it doesn't work for ORALCE. When you use M-> add () to add data, the field is filtered out by the $ this-> _ facade () method. The SQL statement generated in this way cannot be executed. It must be incorrect. As a result, the data cannot be added to the database, so the select () method is also used for filtering.

Again, when I perform single-step debugging and the breakpoint is filtered, the new MODEL is used for the filtering method. This MODEL has an array mapped to fields, this filtering method is compared with this field array. If they are inconsistent, it will be filtered out. After debugging, I found that the new MODEL did not add field ing, And the array is empty, of course, it cannot match the added data fields one by one. This is the key to errors.

The Solution Below is actually very simple, according to the basic MVC structure, whether it is PHP, JAVA or. NET has such a structure, so according to strict standards, the MODEL layer code must be written, that is, to map with the database fields. However, many mysql users do not directly write the code in the MODEL. This kind of habit is used in oracle, and a problem occurs.

5. Write my code for the data table above:

My Action is like this: UserAction. class. php. I only add and search for the Controller as an example, so the code is as follows:

The Code is as follows:
Public function index (){
Header ("Content-Type: text/html; charset = UTF-8 ");

$ M_User = new UserModel ();

$ User_List = $ M_User-> select ();

$ This-> assign ('title', 'user management ');

$ This-> assign ('userlist', $ User_List );

$ This-> display ();
}

// Add a user for submission
Public function Create_Post (){
$ M_User = new UserModel ();
$ Data ['username'] = $ this-> _ post ('username ');
$ Data ['Password'] = md5 ($ this-> _ post ('pwd '));

If ($ M_User-> create ()){
$ Query_Result = $ M_User-> add ($ data );
If (false! ==$ Query_Result ){
$ This-> success ('user added successfully ');
} Else {
$ This-> error ('user added error ');
}
} Else {
Header ("Content-Type: text/html; charset = UTF-8 ");
Exit ($ M_User-> getError (). '[Return]');
}
}

Action explanation:

The Code is as follows:
$ M_User = new UserModel ();

It is best to write this method because it is always written for. NET reasons. Instantiate a specific model. It is strictly required that the User table be operated.

The code for retrieving POST data will not be explained much.

The Code is as follows:
$ M_User-> create ();

This is a ThinkPHP method. It is good and can help you filter out illegal things. We recommend that you use this method.

The Code is as follows:
$ Query_Result = $ M_User-> add ($ data );

This section is about adding data. I am used to specifying the data to be added because this section needs to be instantiated based on $ M_User and filtered out fields. Of course, we only need to do a good job of MODEL code. The following code is not explained. All official documents are available.


My Model is like this: UserModel. class. php

The Code is as follows:
? Protected $ fields = (
'Id', 'username', 'Password'
);


Model explanation: This is the focus. In this way, the new $ M_User ing field array will not be empty, so that it can correspond to POST data, so that the filtering method can be recognized normally without being filtered.

6. After the above operations, the Oracle database operations are completed. Now I can use the methods provided by ThinkPHP to operate data, including paging (limit ), find (), findAll, and so on.

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.