Provides various official and user-released code examples. For code reference, you are welcome to learn how to fix the data insertion BUG in TP + ORACLE and obtain the auto-increment Id. The getLastInsID method is supported.
During Api operations these days, you may find that you can use TP to operate the Oracle database and query, modify, and delete all queries,
However, once an insert operation is executed, an error is always reported. Similar issues such as: http://www.thinkphp1.cn/bug/3286.html
It took some time to study it carefully and found it was a BUG.
The following is my solution:
For version: ThinkPHP3.2.3
BUG fixes:
Modify the file: Db \ Driver \ Oracle. class. php
Find the execute method,
Find: $ this-> initConnect (true );
Add $ bind = $ this-> bind:
Public function execute ($ str, $ fetchSql = false ){
$ Bind = $ this-> bind; // Add this sentence
$ This-> initConnect (true );
Find: foreach ($ this-> bind as $ key => $ val) {
Add $ this-> bind = $ this-> bind? $ This-> bind: $ bind; this sentence:
$ This-> bind = $ this-> bind? $ This-> bind: $ bind; // Add this sentence
Foreach ($ this-> bind as $ key => $ val ){
Find $ this-> lastInsID = $ this-> _ linkID-> lastInsertId ();
Modify it:
// Modify:
// $ This-> lastInsID = $ this-> _ linkID-> lastInsertId ();
$ This-> lastInsID = $ this-> lastInsertId ($ this-> table );
Add the following code to the Oracle. class. php file:
/**
* Obtain the last inserted Oracle ID.
* @ Access public
*/
Public function lastInsertId ($ sequence = ''){
Try {
$ LastInsID = $ this-> _ linkID-> lastInsertId ();
} Catch (\ PDOException $ e ){
// When the driver does not support PDO: lastInsertId ()
Try {
$ LastInsID = 0;
$ SeqPrefix = C ("DB_SEQUENCE_PREFIX ")? C ("DB_SEQUENCE_PREFIX"): 'seq _';
$ Sequence = $ sequence? $ Sequence: $ seqPrefix. $ this-> table;
$ Q = $ this-> query ("SELECT {$ sequence}. CURRVAL as t from dual ");
If ($ q ){
$ LastInsID = $ q [0] ['T'];
}
} Catch (\ Exception $ e ){
// Print "Error! : ". $ E-> getMessage ()."
";
// Exit;
}
}
Return $ lastInsID;
}
Call method:
1. Database Configuration:
'Db _ prefix' => 'tb _ ', // table name PREFIX
'Db _ sequence_prefix' => 'seq _ ', // sequence name prefix. The sequence of each table should be: sequence name prefix + Table Name
'Db _ trigger_prefix' => 'tig _ ', // trigger name prefix
2. Create a user data table first
Table field: id, username, password
3. Then create [sequence + trigger]
---- Create Sequence
Create sequence seq_user
Increment by 1
Start with 1
Nomaxvalue
Nominvalue
Nocache;
---- Create a trigger
Create or replace trigger "tig_user"
Before insert on tb_user
For each row when (new. id is null)
Begin
Select seq_user.nextval into: new. id from dual;
End;
4. In the last step, write the insert data code in UserAction as follows:
$ Data = array (
'Phone' => $ phone,
'Password' => md5 ($ password)
);
$ R = M ('user')-> field (true)-> add ($ data); // execute the insert operation and return the Id of the last insert.
If ($ r ){
// $ R = M ('user')-> getLastInsID (); // obtain the last inserted Id
Echo 'last inserted record: '. $ r;
} Else {
$ This-> error ('Operation failed ');
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB