Solutions that cannot be used by encapsulation methods when connecting Oracle in thinkphp _php tips

Source: Internet
Author: User
Tags oracle database
Recently collected some questions about thinkphp connection Oracle database, there are many friends in accordance with the method to connect MySQL operation, resulting in some methods in the Oreale can not be used normally. For example, the Findall,select method cannot be used to obtain the required data. The Create and add methods cannot be created and written to the database.

In fact, according to the previous problems I have done a few days debugging, found the problem, and successfully in my own small project to use the normal practice, then I will now share my experience.

1, the database connection and the contents of the configuration file I do not say, the above has been explained. I'm only here to illustrate my operation based on an example of a datasheet.
2, the table structure is as follows:

3, there are 3 fields in this table, ID primary key, username username and password password, because the Oracle database to the table name and fields are capitalized, and does not support the ID of the primary key to increase, I only use a different method to achieve this function, such as: ID automatic sequence + The trigger implementation ID is self increasing.

In 4,thinkphp, the action is the controller, model is modeled and the view is embodied in a template way.
First of all, say controller, I only do add and get list of methods introduced.
Secondly, to say the model, here is the main reason for success. why not? thinkphp is a field map, this in the MySQL support is very perfect, basically do not have to write model, but for Oralce, when using M->add () to add data, the field will be $this->_facade () method filter out. This generated SQL statement is not executable, is certainly wrong, resulting in the data is not added to the database, then the use of the Select () method is also filtered.

Again, when I step debugging, when the breakpoint is filtered, the filter method used to the new model, the model will have a field map of the array in the inside, this filtering method is compared with the field array, if inconsistent on the filter, the result I debug found, The new model does not add the field mapping, the array directly empty, of course, and added data field one by one corresponding. That is the key to the mistake.

The following is the solution, in fact very simple, according to the basic MVC structure, whether it is PHP or Java or. NET have such a structure, then according to strict standards, the model layer of code must be written, is to and the database fields to do mapping. But a lot of MySQL, it is not directly to write model inside the code. This habit was used in Oracle, and there was a problem.

5, write down my code for the data table above:
My action is this: UserAction.class.php. Controller I only do an example of adding and finding, so the code is as follows:

Copy Code code 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 ', ' User Management ');

$this->assign (' userlist ', $User _list);

$this->display ();
}

Add user Submit Processing
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 (' User add success ');
} else {
$this->error (' User add error ');
}
} else {
Header ("content-type:text/html; Charset=utf-8 ");
Exit ($M _user->geterror (). ' [<a href= ' Javascript:history.back () ' > Return </a>] ');
}
}

Action Explanation:
Copy Code code as follows:

$M _user=new Usermodel ();

This method is best written because it is done. NET reason, has been so written. For the specific model of the instantiation, it is strictly stipulated that I will be operating on the user table.
The code that gets the post data is not much explained.
Copy Code code as follows:

$M _user->create ();

This is a method of thinkphp, very good, can help you to filter out illegal things, recommended to use.
Copy Code code as follows:

$Query _result = $M _user->add ($data);

This section is the addition of data, I am accustomed to specifying the data to be added, also because this paragraph needs to be instantiated according to $m_user, and filter the fields. Of course, as long as we do the code model, there will be no problem. The following code does not explain. Official documents are available.

My model is like this: UserModel.class.php
Copy Code code as follows:

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

Model Explanation: This is the focus, this is so, new out of the $m_user mapping field array will not be empty, so as to and post data to correspond, will let the filtering method is normally recognized, not filtered.

6, after the above operation, the database operation for Oracle is complete, and I can now also use the thinkphp provided to manipulate the data, including pagination (limit), find (), FindAll and so on.

Connecting to MySQL may not be a problem, but in Oracle, when the encapsulated method cannot be invoked, be sure to include the definition of the field in the model layer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.