Does thinkphp5 support oracle?
First of all, we know that php fully supports oracle, then thinkphp5 as a php framework can also fully support oracle.
How does thinkphp5 connect to oracle?
Database: ray
Table structure: ray_user
CREATE TABLE IF NOT EXISTS ray_user ( user_id int(11) unsigned NOT NULL AUTO_INCREMENT, user_name varchar(10) NOT NULL, user_pwd varchar(40) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
Transfer data in the table: ray_user
INSERT INTO ray_user (user_id, user_name, user_pwd) VALUES (1, ‘updatename’, ‘ray’), (2, ‘testname’, ‘testpwd’),
1. CURD operation in mysql environment
Database configuration database.php
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'ray', // 用户名 'username' => 'root', // 密码 'password' => '', // 你的密码 // 端口 'hostport' => '3306', // 连接dsn 'dsn' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'ray_', // 数据库调试模式 'debug' => true, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库读写是否分离 主从式有效 'rw_separate' => false, // 读写分离后 主服务器数量 'master_num' => 1, // 指定从服务器序号 'slave_no' => '', // 是否严格检查字段是否存在 'fields_strict' => true, // 数据集返回类型 'resultset_type' => 'array', // 自动写入时间戳字段 'auto_timestamp' => false, // 时间字段取出后的默认时间格式 'datetime_format' => 'Y-m-d H:i:s', // 是否需要进行SQL性能分析 'sql_explain' => false, ];
Controller User.php
<?php namespace app\index\controller; use think\Controller; use app\index\model\User as US; class User extends Controller { public function index() { $obj_user = new US; // 查找 $data = $obj_user->operateUser("find",null,"1"); var_dump($data); // 更新 $updateData = [ 'user_name' => 'updatename' ]; $result = $obj_user->operateUser("update",$updateData,"1"); var_dump($result); // 新增 $insertData = [ 'user_name' => 'testname', 'user_pwd' => 'testpwd' ]; $result = $obj_user->operateUser("insert",$insertData); var_dump($result); // 删除 $result = $obj_user->operateUser("delete",null,'2'); var_dump($result); } }
Model User.php
<?php namespace app\index\model; use think\Model; class User extends Model { public function operateUser($directive,$data = null,$user_id = null) { if($directive == "find" && $user_id != null) { return User::where('user_id',$user_id)->find(); } else if($directive == "insert" && $data != null) { return User::save($data) ? 1 : 0; } else if($directive == "update" && $data != null && $user_id != null) { return User::where('user_id',$user_id)->find()->save($data) ? 1 : 0; } else if($directive == "delete" && $user_id != null) { return User::where('user_id',$user_id)->delete() ? 1 : 0; } else { return null; } } }
2. CURD operation in oracle environment
Database configuration file database.php
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- return [ // 数据库类型 'type' => '\think\oracle\Connection', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'orcl', // 用户名 'username' => 'Scott', // 密码 'password' => '', // 你的密码 // 端口 'hostport' => '1521', // 连接dsn 'dsn' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'ray_', // 数据库调试模式 'debug' => true, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'deploy' => 0, // 数据库读写是否分离 主从式有效 'rw_separate' => false, // 读写分离后 主服务器数量 'master_num' => 1, // 指定从服务器序号 'slave_no' => '', // 是否严格检查字段是否存在 'fields_strict' => true, // 数据集返回类型 'resultset_type' => 'array', // 自动写入时间戳字段 'auto_timestamp' => false, // 时间字段取出后的默认时间格式 'datetime_format' => 'Y-m-d H:i:s', // 是否需要进行SQL性能分析 'sql_explain' => false, ];
3. Query records based on the specified ID
Since Oracle table names and field names need to be added with double quotes, rewrite them The parseSqlTable and parseWhereItem methods in thinkphp\library\db\Builder.php. After the rewriting is completed, query the record based on the ID and OK.
... /** * 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写) * @access protected * @param string $sql sql语句 * @return string */ protected function parseSqlTable($sql) { return '"'. strtoupper($this->query->parseSqlTable($sql)).'"'; //// 前后加上双引号并将表明设置为大写 } ...... // where子单元分析 protected function parseWhereItem($field, $val, $rule = '', $options = [], $binds = [], $bindName = null) { // 字段分析 $key = $field ? '"'. $this->parseKey($field, $options, true) .'"' : ''; ////前后加上双引号 // 查询规则和条件 if (!is_array($val)) { $val = is_null($val) ? ['null', ''] : ['=', $val]; } list($exp, $value) = $val; ...
Rewritten the controller and model layer methods:
##Controller Users.php
<?php namespace app\index\controller; use think\Controller; use app\index\model\Users as US; class Users extends Controller { public function index() { // 查询 $obj_users = new US; $data = $obj_users->operateUser("find",null,"1"); var_dump($data); // 更新 $updateData = [ 'NAME' => "updateora", 'PWD' => "newpwd" ]; $result = $obj_users->operateUser("update",$updateData,"1"); var_dump($result); // 插入 $insertData = [ 'NAME' => 'testname', 'PWD' => 'testpwd' ]; $result = $obj_users->operateUser("insert",$insertData); var_dump($result); // 删除 $result = $obj_users->operateUser("delete",null,'18'); var_dump($result); } }
Model Users.php
<?php namespace app\index\model; use think\Model; class Users extends Model { public function operateUser($directive,$data = null,$ID = null) { if($directive == "find" && $ID != null) { return Users::where('ID',$ID)->find(); } else if($directive == "insert" && $data != null) { /*$id = Users::getLastInsID('SEQUSERS')-2; var_dump($id); $data['ID'] = $id;*/ return Users::save($data,[],'SEQUSERS') ? 1 : 0; // 注意这里传参 } else if($directive == "update" && $data != null && $ID != null) { return Users::where('ID',$ID)->find()->save($data) ? 1 : 0; } else if($directive == "delete" && $ID != null) { return Users::where('ID',$ID)->delete() ? 1 : 0; } else { return null; } } }
After testing and updating the data, the next step is the most troublesome addition. Because the MySQL primary key can be auto-incremented by adding the A-I attribute to the PK, while Oracle needs to achieve it through a trigger. A simple implementation method is used below.
Trigger, sequence realizes Oracle primary key auto-increment.
CREATE OR REPLACE TRIGGER TRIUSERS BEFORE INSERT ON SCOTT.USERS FOR EACH ROW WHEN ( new.id is null ) begin select SEQUSERS.nextval into:new.id from dual; end; create sequence SEQUSERS minvalue 1 maxvalue 999999999999999999999999999 start with 1 increment by 1 nocache;
Need to rewrite the getLastInsId() method in think-oracle\src\Connection.php
/** * 获取最近插入的ID * @access public * @param string $sequence 自增序列名 * @return string */ public function getLastInsID($sequence = null) { $pdo = $this->linkID->query("select {$sequence}.nextval as id from dual"); $pdo = $this->linkID->query("select {$sequence}.currval as id from dual"); $result = $pdo->fetchColumn(); $pdo = $this->linkID->query("alter sequence {$sequence} increment by -1"); $pdo = $this->linkID->query("select {$sequence}.nextval as id from dual"); $pdo = $this->linkID->query("alter sequence {$sequence} increment by 1"); return $result; }
The above is the detailed content of Does thinkphp5 support oracle?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.
