Home >Backend Development >PHP Tutorial >Solution to the problem that the encapsulation method cannot be used when connecting to Oracle in thinkphp_PHP tutorial

Solution to the problem that the encapsulation method cannot be used when connecting to Oracle in thinkphp_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:45800browse

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:

Copy code 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', '用户管理');

        $this->assign('UserList', $User_List);

        $this->display();
}

    //添加用户提交处理
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('用户添加成功');
            } else {
                $this->error('用户添加错误');
            }
        } else {
            header("Content-Type:text/html; charset=utf-8");
            exit($M_User->getError() . ' [ 返 回 ]');
        }
}

Action解释:
复制代码 代码如下:

$M_User=new UserModel();

这个方法最好这么写,因为做.NET的原因,一直都这么写的。针对具体的模型进行实例化,严格规定我就要对User表进行操作了。
获取POST数据的代码就不多解释了。
复制代码 代码如下:

$M_User->create();

这是ThinkPHP的一个方法,很好,可以帮你过滤掉非法的东西,建议使用。
复制代码 代码如下:

$Query_Result = $M_User->add($data);

这一段就是数据的添加,我习惯指定要添加的数据,也是因为这一段需要根据$M_User实例化,并过滤字段。当然了,我们只要做好MODEL的代码,就不会有问题。下面的代码就不解释。官方文档都有。

我的Model是这样的:UserModel.class.php
复制代码 代码如下:

?protected $fields = array(
            'id', 'username', 'password'
        );

Model explanation: This is the key point. This way, the mapping field array of $M_User generated by new will not be empty, so that it can correspond to the POST data. Let the filtering method recognize it normally and not be filtered.

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

There may be no problem connecting to MySQL, but in Oracle, when the encapsulated method cannot be called, the field definition must be added to the model layer.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327668.htmlTechArticleRecently, we have collected some problems about THinkPHP connecting to Oracle database. Many friends follow the method of connecting to mysql, resulting in There are some methods that don't work properly in Oreale. For example...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn