ThinkPHP 连接Oracle数据库的详细教程(2)
最近收集了一些关于THinkPHP连接Oracle数据库的问题,有很多朋友按照连接mysql的方法来操作,导致有一些方法在Oreale中无法正常使用。比如说:findAll,Select方法无法使用,获取不到需要的数据。Create和add方法无法创建和写入数据到数据库中。
最近收集了一些关于THinkPHP连接Oracle数据库的问题,有很多朋友按照连接mysql的方法来操作,导致有一些方法在Oreale中无法正常使用。比如说:findAll,Select方法无法使用,获取不到需要的数据。Create和add方法无法创建和写入数据到数据库中。
其实根据以前问题我做了几天调试,找到了问题所在,并成功在我自己一个小项目练习中使用正常,那么现在就将我的经验分享给大家。
1,的连接及配置文件的内容我就不说了, 上面已经做了解释。我这里只根据一个数据表的例子来说明我的操作。
2,表结构如下:
3,这个表中有3个字段,ID主键,用户名username和密码pass,因为oracle数据库把表名和字段都是转成大写的,同时不支持ID主键自增,我只有使用另外的方法来实现这个功能,比如:ID自动序列+触发器实现ID自增。
4, ThinkPHP中,是控制器,Model是模型,视图是以模板方式体现的。
首先,说控制器,我只做增加和获取列表的方法介绍。
其次,说模型,这里才是成功的主要原因。为什么?ThinkPHP是有字段映射的,这个在对MYSQL的支持非常完美,基本不用写MODEL,但是对ORALCE就不行了,当使用M->add()来添加数据时,字段会被$this->_facade()方法过滤掉。这样生成的SQL语句就是没法执行的,肯定是错误的,导致数据添加不到数据库中,那么使用select()方法也是一样被过滤。
再次,当我单步调试时,断点被过滤的时候,过滤方法使用到了new出来的MODEL,这个MODEL会有一个字段映射的数组在里面,这个过滤方法就是和这个字段数组进行对比,如果不一致就过滤掉,结果我调试发现,new出来的MODEL根本没有把字段映射加进去,数组直接为空,当然就没法和添加的数据字段一一对应了。这就是错误的关键。
下面就来说解决方法,其实很简单,按照基本的MVC结构,不管是PHP还是JAVA还是.NET都有这样的结构,那么按照严格的标准,MODEL层的代码是必须写的,就是要和数据库的字段做映射。但是很多用mysql的,就直接没有去写MODEL里面的代码。这种习惯被用到了oracle中,就出了问题。
5, 下面针对我上面的数据表写出我的代码:
我的Action是这样的:UserAction.class.php。控制器我只对添加和查找做例子,因此代码如下:
代码如下 | 复制代码 |
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 = ( 'id', 'username', 'password' ); |
Model解释:这才是重点,这有这样,new出来的$M_User的映射字段数组才不会为空,这样才能和POST的数据进行对应,才会让过滤方法正常识别,不被过滤。
6,经过了以上的操作,针对Oracle的数据库操作就完成了,我现在也可以任意使用ThinkPHP提供的方法来操作数据了,包括分页(limit),find(),findAll等等。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.
