Method of executing native SQL statement in thinkPHP framework

  • 2021-08-10 07:11:26
  • OfStack

This paper illustrates the method of executing native SQL statements in thinkPHP framework. Share it for your reference, as follows:

How to execute the native sql statement in thinkphp?


$Model = new Model();// Or  $Model = D();  Or  $Model = M();
$sql = "select * from `order`";
$voList = $Model->query($sql);

Just need an empty model of new1 to inherit the methods in Model.

Note that query is a search function, and execute is an addition, deletion and modification function

Query and read instances of property values:


$sql = "select * from goods";
$Model = M();
$result = $Model->query($sql);
foreach ($result as $k=>$val){
$goods_id = $val["goods_id"];
}

The model of tP can support native SQL operation and provide two methods: query and execute. Why should native SQL distinguish two methods? There are two reasons:

1. Different return types

query is used for querying, and returns a dataset like select or findall1, so you can directly output the query result of query by using the volist tag in the template

execute is used for write operations and returns the status or the number of records affected

2. Read and write statistics

In order to count the current data read and write times, separate the read and write operations of the database (corresponding to query and execute)

Using native SQL is simple, and we don't even need to instantiate any models, such as:


$Model = new Model(); //  Instantiation 1 Empty model 

The following methods are equivalent


$Model = D();//  Or  $Model = M();
//  Perform native below SQL Operation 
$Model->query('select * from think_user where status=1');
$Model->execute('update think_user set status=1 where id=1');

If you instantiate a model, you can still perform native SQL operations without being affected, for example:


$User = D('User');
$User->query('select * from think_user where status=1');
$User->execute('update think_user set status=1 where id=1');

In this case, we can simplify the writing of SQL statement, for example:


$User->query('select * from __TABLE__ where status=1');
$User->execute('update __TABLE__ set status=1 where id=1');

The system will automatically replace __TABLE__ with the data table name corresponding to the current model, and the actual data table is determined by the model.

Generally speaking, we all use native SQL operation to realize some operations that are difficult to realize in ORM and CURD. In addition, if SQL is not complicated, the efficiency difference between native SQL and coherent operation is very small, and ORM implementation of TP itself is also quite efficient.

For more readers interested in thinkPHP related contents, please check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of ThinkPHP Common Methods", "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Introduction to Zend FrameWork Framework" and "Summary of PHP Template Technology".

Hope that this article is based on ThinkPHP framework of PHP programming help.


Related articles: