Home > Article > Backend Development > Solution to the problem that the encapsulation method cannot be used when connecting to Oracle in thinkphp_PHP tutorial
Recently, we have collected some problems about THinkPHP connecting to Oracle database. Many friends follow the method of connecting to mysql, resulting in some methods that cannot be used normally in Oreale. For example: findAll, Select methods cannot be used, and the required data cannot be obtained. The Create and add methods cannot create and write data to the database.
In fact, I did a few days of debugging based on previous problems, found the problem, and successfully used it normally in a small project practice of my own, so now I will share my experience with everyone.
1. I won’t go into details about the database connection and configuration file. They have been explained above. I will only illustrate my operation based on an example of a data table.
2. The table structure is as follows:
3. There are 3 fields in this table, ID primary key, username username and password password, because Oracle database converts table names and fields to uppercase At the same time, it does not support the ID primary key auto-increment. I can only use other methods to achieve this function, such as: ID automatic sequence + trigger to achieve ID auto-increment.
4. In ThinkPHP, Action is the controller, Model is the model, and the view is represented by a template.
First of all, talking about the controller, I will only introduce the methods of adding and getting the list.
Secondly, talking about the model, this is the main reason for success. Why? ThinkPHP has field mapping, which is perfect for supporting MYSQL. You basically don’t need to write MODEL, but it doesn’t work for ORALCE. When using M->add() to add data, the fields will be $this-> _facade() method filters out. The SQL statement generated in this way cannot be executed and must be wrong. As a result, the data cannot be added to the database, and the select() method will also be filtered.
Again, when I single-step debug and the breakpoints are filtered, the filtering method uses the new MODEL. This MODEL will have an array of field mappings in it. This filtering method is related to this field. Compare the arrays and filter them out if they are inconsistent. As a result, I debugged and found that the new MODEL did not add field mapping at all, and the array was directly empty. Of course, it could not correspond to the added data fields one by one. This is the crux of the mistake.
Let’s talk about the solution. It is actually very simple. According to the basic MVC structure, whether it is PHP, JAVA or .NET, all have such structures. Then according to strict standards, the code of the MODEL layer must be written Yes, it needs to be mapped to the fields of the database. But many people who use mysql simply do not write the code in MODEL. When this habit is used in Oracle, there is a problem.
5. Write my code below for the data table above:
My Action is like this: UserAction.class.php. For the controller, I only make examples for adding and searching, so the code is as follows: