Home > Article > PHP Framework > ThinkPHP how to increment and decrement multiple fields at the same time
“This article will talk about how to increase and decrease multiple fields at the same time
”
Xiao Q recently went for an interview, and then he asked the question, how to increase and decrease multiple fields at the same time.
Little Q couldn't answer for a while, and the final result was to go home and wait for notification....
Kaka will give Xiao Q a simple answer to this question.
ThinkPHP, then Kaka will first use ThinkPHP to solve this problem.
First of all, to solve this problem, you need to have a certain understanding of the inc and setInc of the framework. In the framework, these two functions are used to increment or decrement. But there is a difference between the two. inc is a method in the Db class, and setInc actually calls the method in the model, but in the end,thinkphp/library/think/db/Query is used .phpMethod of this file.
setInc like Xiao Q? This is what Xiao Q thought when he got this question.
When visiting, he will appear mercilessly and give Little Q a painful blow.
So this method is not feasible, but what should I do if I still want to implement this function!
Don’t worry, Kaka will take you to visit the New World.
Use two inc methods directly to increment or decrement multiple fields, so the idea of KaKa is also simply based on the source code.
Or maybe everyone has used two where methods in one query when using the thinkphp framework! In fact, the ideas are the same since both where can implement queries.
Then those two incs should also be able to implement multiple fields for auto-increment or auto-decrement.
So Kaka’s final solution is the solution as shown above.
#In the process, Kaka still went through boring rummaging about inc implementation process.
In the figure below, we mainly look at the explanations given about the parameters. You can see that the first parameter can be an array or a string.
But according to the code, you will find that although multiple fields are supported to increase or decrease, the step size is one value.
So the method provided by the framework is which can increase or decrease multiple fields at the same time, but the value can only be fixed
.
If you want to implement multiple fields and multiple steps, you need to modify the source code to solve this problem.
The following is the content of Kaka after modifying the source code. You can compare it with the picture of the inc source code Make comparisons.
The source code modified by Kaka is mainly the circled area, because the value of step is directly defined as 1 in the source code.
So you need to modify this block and use is_array to detect whether the variable is an array.
After the above operations, you can achieve self-increment or self-decrement for multiple fields and multiple steps.
It is not recommended for everyone to use Kaka to directly modify the source code. We just need to learn to find a solution in the source code for a problem.
So for the problem of how to increment and decrement multiple fields at the same time, it is recommended to use solution one.
After all, this situation is in the minority. If you change the basics of the framework, you will get messed up.
If you want to solve the problem, you don’t need to read this section. Yes, just watch the first section and you will be able to solve your problem perfectly.
Kaka likes to think about a problem and use multiple solutions to solve it.
Then Kaka will use SQL statements to conduct an in-depth analysis of this problem.
Now that we have all implemented the implementation plan in the first section, there is a method fetchSql() in the framework that can directly print out the sql statement.
Then let’s take a look at what this sql statement looks like.
<span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">UPDATE</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">`table`</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">SET</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">`filed1`</span> = <span class="hljs-string" style="color: #98c379; line-height: 26px;">`filed1`</span> + <span class="hljs-number" style="color: #d19a66; line-height: 26px;">200</span> , <span class="hljs-string" style="color: #98c379; line-height: 26px;">`filed2`</span> = <span class="hljs-string" style="color: #98c379; line-height: 26px;">`filed2`</span> + <span class="hljs-number" style="color: #d19a66; line-height: 26px;">86</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">WHERE</span> <span class="hljs-string" style="color: #98c379; line-height: 26px;">`time`</span> <br/><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">BETWEEN</span> <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1609689600</span> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">AND</span> <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1609775999</span><br/></code>
The above is the SQL statement printed out using the method provided by the framework. The table name and field Kaka have been changed. You can directly modify them to the fields that you need to increase or decrease.
Using this SQL statement can solve the topic of this article, so Kaka did not give a series of screenshots of the print results. If you are interested, you can test it yourself.
As the saying goes, just talk and don’t practice fake tricks. You still need to practice a lot on your own.
The above is implemented in the framework using native sql to simultaneously increase and decrease multiple fields and multiple steps.
This problem is actually very simple in nature. The framework also provides corresponding methods. You only need to directly Just call it.
The problem is that you know how many self-increment or self-decrement methods are provided by the framework. The framework provides two methods: inc and setInc.
If you only know setInc, wouldn’t it be a mistake? So if you have nothing to do, you should read more about the source code and documentation. It will only do you good and not bad.
Another question is about Kaka’s article mentioning the use of a new method to achieve simultaneous increment and decrement of multiple fields. This method has been modified at the bottom of the framework.
This implementation method is not recommended. Modifying the source code is only for testing and to improve your ability to read the source code, rather than making fearless modifications at the bottom of the framework.
“Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Silk help. My name is Kaka, see you next time.
”
The above is the detailed content of ThinkPHP how to increment and decrement multiple fields at the same time. For more information, please follow other related articles on the PHP Chinese website!