Home > PHP Framework > ThinkPHP > Summarize the common functions of thinkphp6

Summarize the common functions of thinkphp6

WBOY
Release: 2022-05-07 21:35:48
forward
4079 people have browsed it

This article brings you relevant knowledge about thinkphp, which mainly organizes some commonly used functions, including obtaining form data, setting multi-application mode, template rendering, etc., as follows Let's take a look, I hope it will be helpful to everyone.

Summarize the common functions of thinkphp6

Recommended study: "PHP Video Tutorial"

The first step is to change the composer image

Alibaba mirror:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

Laravel China mirror : composer config -g repo.packagist composer https://packagist.laravel-china.org

China Full Mirror: composer config -g repo.packagist composer https://packagist. phpcomposer.com9

In the code cloud or git download case, it is found that it cannot run without a vendor

composer install --ignore-platform-reqs

or

composer update --ignore-platform-reqs

Tp6 use

1. Download tp6
composer create-project topthink/think tp

2. Set multi-application mode
The default method of tp6 is to enter app/controller for single-application access. If you need to do multi-application development (for example: http://***.com/admin, http://***.com/index) need to enable multi-application mode

composer requiretopthink/think-multi-app

php think build application name (for example: index or admin)

3. Template rendering

tp3: $this->display();tp5: return $this->fetch();tp6: return View::fetch('index');
Copy after login

tp6 lacks many dependency packages by default and needs to be downloaded

composer require topthink/think-view

Controller introduction

use think\facade\View;

4. Template jump redirection
Summarize the common functions of thinkphp6

composer require liliuwei/thinkphp-jump

Controller introduction

Header introduction: use \liliuwei\think\Jump;

Intra-class introduction: use Jump;

If an error is reported:

Check if app/config/jump.php has settings:

'dispatch_success_tmpl' => app()->getRootPath().'/vendor/qeq66/think-jump/template/jump.html','dispatch_error_tmpl' => app()->getRootPath().'/vendor/qeq66/think-jump/template/jump.html'
Copy after login

5. Get form data
Controller introduction

use think\facade\Request;$code = Request::param('code');
Copy after login

or

$code = input("code");
Copy after login

6, digital verification code
composer require topthink/think-captcha

Find the global middleware middleware.php in the application app directory file, open the code \think\middleware\SessionInit::class commented below

7. Upload image processing image
composer require topthink/think-image

8, mysql select query
Summarize the common functions of thinkphp6

##Transitioned from tp5, the default select query returns a two-dimensional array, and tp6 returns a data set, although the official said that it operates with arrays There is basically no difference

But sometimes arrays are better to use, for example

##arr[k][0] = "test" This kind of indirect modification will report an error in the default returned data set, but Arrays can be operated like this 1.

 db::name('ceshi')->select()->toArray();
Copy after login

2. Modify the BaseQuery.php of /vendor/topthink/think-orm/src/db in the tp6 directory


Summarize the common functions of thinkphp6 Modify the example as shown in the picture, delete the red box in the picture, and add a line below

$resultSet = $this->connection->select($this);
Copy after login

return $resultSet;
Copy after login

9, Pagination

$list = db::name('admin_menu')->where($where)->paginate(['list_rows'=> 10,'query' => request()->param(),]);
Copy after login
Use the paginate method to obtain pagination data, query the subscript value that cannot be added to the collection

The query condition needs to add 'query' => request()->param (),

Solution:

php side:

// An highlighted block$list = db::name('admin_menu')->where($where)->paginate(['list_rows'=> 10,'query' => request()->param(),]);$new_arr = array();foreach($list as $k=>$v){$v[$k]['erji_menu'] = “案例”;$new_arr[] = $v;}
// 获取分页显示$page = $list->render();// 模板变量赋值View::assign('list', $new_arr);View::assign('page', $page);
Copy after login

html side

{$page|raw}

Paging reference class modification

tp6\vendor\topthink\think-orm\src\paginator\driver\Bootstrap.php

10. New data difference

Compare tp5 The save method is used to update the add method is used to add tp6 save is both an update and an add add method is deleted

db::name('ceshi')->where(array('tz_id'=>$post['tz_id']))->save($users);db::name('ceshi')->save($users);
Copy after login

Adding the primary key where is updating without adding the primary key is adding, but there will be problems if the execution is successful and it will only return 0,1 will not return the primary key id of the added data like the tp5 add method

/**
 * 插入记录
 * @access public
 * @param array   $data         数据
 * @param boolean $getLastInsID 返回自增主键
 * @return integer|string
 */public function insert(array $data = [], bool $getLastInsID = false){
    if (!empty($data)) {
        $this->options['data'] = $data;
    }

    return $this->connection->insert($this, $getLastInsID);}
Copy after login

The add method was changed to the insert method. Although the comment says that it returns the auto-incremented primary key, I still didn’t test it here. I got the auto-incremented primary key. I’m not sure whether it’s a version problem or something. I won’t go into details here. If you don’t encounter it, just assume I didn’t say it
/**
 * 插入记录并获取自增ID
 * @access public
 * @param array $data 数据
 * @return integer|string
 */public function insertGetId(array $data){
    return $this->insert($data, true);}
Copy after login

. If you can’t get it, you can only continue to search and find that there is an insertGetId that you can get. That is to say, in order to develop quickly, we usually use save to solve the problem. When we need to get the auto-incremented primary key, we change it to insertGetId

.

11、tp6 高级查询and和or同时使用
Summarize the common functions of thinkphp6
tp6针对and查询和or查询有快捷方法
Summarize the common functions of thinkphp6
但是这些快捷方法会有很多局限性,当我们对数据进行一系列的复杂查询时这些快捷方法就会出现很多问题例如:

$where1[] = ["order_khname","like",'%'.$keywords.'%'];$where2[] = ["order_khqq","like",'%'.$keywords.'%'];$where3[] = ["order_khmobile","like",'%'.$keywords.'%'];$where[] = ["order_type","=",$ddzt];$list = db::name('ceshi')
    ->where($where)
    ->whereOr([$where1,$where2,$where3])
    ->paginate([
        'list_rows'=> 10,
        'query' => request()->param(),
    ]);
Copy after login

这里我们想要的sql格式为:

SELECT * FROM `dc_ceshi` WHERE  `order_type` = 0 and ( ( `order_khname` LIKE '%1%' )  OR ( `order_khqq` LIKE '%1%' )  OR ( `order_khmobile` LIKE '%1%' )) LIMIT 0,10
Copy after login

实际生成的格式为:

SELECT * FROM `dc_ceshi` WHERE  `order_type` = 0 OR ( `order_khname` LIKE '%1%' )  OR ( `order_khqq` LIKE '%1%' )  OR ( `order_khmobile` LIKE '%1%' ) LIMIT 0,10
Copy after login

这里就可以用到闭包查询(注:这只是我学习tp6的时候遇到的问题然后在tp6文档里面找到的解决方式,直接用原生sql也是可以解决,把where条件直接改成一个自定义的字符串也可以解决,用tp5的连接方式也应该可以但是我没试)

$list = db::name('ceshi')
    ->where(function ($query) use ($keywords){
            $query->where("order_khname","like",'%'.$keywords.'%')
                ->whereOr("order_khqq","like",'%'.$keywords.'%')
                ->whereOr("order_khmobile","like",'%'.$keywords.'%');
        })
    ->where($where)
    ->paginate([
    'list_rows'=> 10,
    'query' => request()->param(),]);
Copy after login

这时候得到sql格式为:

SELECT * FROM `dc_ceshi` WHERE  (  `order_khname` LIKE '%1%' OR `order_khqq` LIKE '%1%'  OR `order_khmobile` LIKE '%1%' )  AND `order_type` = 0 LIMIT 0,10
Copy after login

已经满足了我的需求,但是还是有一个问题就是如果keywords这个变量没有值的时候他查询的语句格式为:

SELECT * FROM `dc_ceshi` WHERE  (  `order_khname` LIKE '%%' OR `order_khqq` LIKE '%%'  OR `order_khmobile` LIKE '%%' )  AND `order_type` = 0 LIMIT 0,10
Copy after login

这种情况只需要在加个if判断:

$list = db::name('ceshi')
    ->where(function ($query) use ($keywords){
        if($keywords){
            $query->where("order_khname","like",'%'.$keywords.'%')
                ->whereOr("order_khqq","like",'%'.$keywords.'%')
                ->whereOr("order_khmobile","like",'%'.$keywords.'%');
        }
        })
    ->where($where)
    ->paginate([
    'list_rows'=> 10,
    'query' => request()->param(),]);
Copy after login

12、tp6 find查询变动
tp5、tp3我们查询一个表是否存在数据可以直接查询一条

$datafind = db::name('ceshi')->find();
Copy after login

tp6 find查询必须增加where条件或者order排序,为什么不知道。有大神知道指点下O(∩_∩)O哈哈~

$datafind = db::name('ceshi')->order('ID DESC')->find();
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of Summarize the common functions of thinkphp6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template