学习记录
thinkphp5.1对模型的操作,增删查改
用最常见的文章操作来举例增加文章增加文章页面 增加文章控制器 增加文章模型在模型中定义模型方法<?phpnamespace app\admin\model;use think\Model;class Article extends Model { /** * @description 发布文章 * */ public function createArticle($article){ $this->save(.
2021-04-29 06:24:47
327
                <blockquote> 

用最常见的文章操作来举例

增加文章

  • 增加文章页面
  • 增加文章控制器的操作方法
  • 增加文章模型

在模型中定义模型方法

<?php
namespace app\admin\model;

use think\Model;

class Article extends Model {

/**
 * @description 发布文章
 * */
public function createArticle($article){
    $this-&gt;save([
        'title'   =&gt;  $article['title'],
        'desc'    =&gt;  $article['desc'],
        'content' =&gt;  $article['content'],
        'status'  =&gt;  $article['status'],
        'create_time' =&gt; date("Y-m-d H:i:s")
    ]);
}

}

在控制器中判断是否有request参数,如果存在参数则存储数据库,否则直接渲染页面

<?php

namespace app\admin\controller;

class Article extends Base {

/**
 * @description 发布文章控制器
 * @如果获取到数据,则添加到数据库,否则渲染页面
 * */
public function create(){
    // 获取参数请求参数
    $title = $this-&gt;request-&gt;post('title');
    $desc = $this-&gt;request-&gt;post('desc');
    $status = $this-&gt;request-&gt;post('status');
    $content = $this-&gt;request-&gt;post('content');

    // 如果存在参数存储数据库
    if($title &amp;&amp; $desc &amp;&amp; $status &amp;&amp; $content){
        $this -&gt; setArticle($title,$desc,$status,$content);
        $res = ['code' =&gt; 1 , 'msg' =&gt; '发布成功' ];
        return $res;
    }

    return $this -&gt; fetch();
}

/**
 * @description 发布文章
 */
public function setArticle($title,$desc,$status,$content){
    $articleModel = new \app\admin\model\Article;//实例化模型
    //操作模型方法来存储数据库
    $articleModel -&gt; createArticle([
        'title'  =&gt; $title,
        'desc'   =&gt; $desc,
        'status' =&gt; $status,
        'content'=&gt; $content
    ]);
}

}

页面上点击提交按钮之后请求当前页面的控制器,传入参数获取返回值

$('#submit').click(function(){
    let title = $("input[name='title']").val();
    let desc = $("textarea[name='desc']").val();
    let status = $("input[name='status']:checked").val();
    let content = tinyMCE.activeEditor.getContent();
    let data = { title,desc,status,content };
    $.post(location.href,data,(res)=>{
        if(res.code == 1){
            localtion.href = "{:url('article/index')}";
        	layer.msg(res.msg);	
        }
    })
})

查询文章

  • 文章列表页面
  • 文章列表控制器的操作方法
  • 文章查询模型

在模型中定义模型方法

主要分为两类:【查询列表(筛选)】和【查询详情】

  • 查询列表通过titlestatus来筛选,如果都未传入则查询所有
  • 查询详情通过id查询
<?php
namespace app\admin\model;

use think\Model;

class Article extends Model {

/**
 * @description 查询文章列表
 * @param Array $params ['status','title']  
 * status 1[草稿]  2[已发布]  空[都查询]
 * */
public function getArticle($params=[]){
    if(!(empty($params))){
        if(isset($params['status'])) $res = $this-&gt;where('status',$params['status'])-&gt;select();
        if(isset($params['title'])) $res = $this-&gt;where('title',$params['title'])-&gt;select();
    }else{
        $res = $this-&gt;select();
    }
    
    return $res;
}

/**
 * @description 查询文章详情
 * @param String $id id
 * */
public function getArticleById($id){
    $article = $this-&gt;where('id',$id)-&gt;find();

    return $article ;
}

}

在控制器中获取列表数据并传给页面

<?php

namespace app\admin\controller;

class Article extends Base {

/**
 * @description 文章列表控制器
 * 根据url参数title或者status来筛选文章列表
 */
public function index(){
    // 获取文章列表
    $list = $this -&gt; getArticle();
    // 获取筛选条件
    $title = input('title');
    $status = input('status');
    if($title || $status){
        if($status) $params = ['status' =&gt; $status];
        if($title) $params = ['title' =&gt; $title];
        $list = $this -&gt; getArticle($params);
    }
    
    // 返回给页面数据
    $this -&gt; assign([
        'title' =&gt; $title,
        'status' =&gt; $status,
        'list' =&gt; $list
    ]);

    return $this -&gt; fetch();
}

/**
 * @description 查询文章【操作模型方法】
 */
public function getArticle($params=[]){
    $articleModel = new \app\admin\model\Article;
    $list = $articleModel -&gt; getArticle($params);
    return $list;
}

}

页面中是通过点击搜索按钮改变href值并将筛选条件携带在url中,控制器通过读取url参数来筛选

 


删除文章

  • 列表页添加一个删除按钮
  • 文章删除模型

删除功能页面操作是:页面列表上有一个删除按钮,通过点击该按钮拿到文章id传给当前页面控制器,在控制器中拿到id执行模型中的删除方法

 在模型中定义模型方法

<?php
namespace app\admin\model;

use think\Model;

class Article extends Model {

/**
 * @description 删除文章
 * @param String $id id
 * */
public function delArticl($id){
    $article = $this -&gt; get($id);
    $article -&gt; delete();

    return $article;
}

}

 在控制器中获取当前id执行模型中的删除方法

<?php

namespace app\admin\controller;

class Article extends Base {

/**
 * @description 文章列表控制器
 * post请求传入id删除文章
 * 根据url参数title或者status来筛选文章列表
 */
public function index(){
    // 获取id删除文章
    $id = $this-&gt;request-&gt;post('id');
    if($id){
        $this -&gt; delArticle($id);
        $res = ['code' =&gt; 1 , 'msg' =&gt; '删除成功' ];
        return $res;
    }
    // 获取文章列表
    $list = $this -&gt; getArticle();
    // 获取筛选条件
    $title = input('title');
    $status = input('status');
    if($title || $status){
        if($status) $params = ['status' =&gt; $status];
        if($title) $params = ['title' =&gt; $title];
        $list = $this -&gt; getArticle($params);
    }
    
    // 返回给页面数据
    $this -&gt; assign([
        'title' =&gt; $title,
        'status' =&gt; $status,
        'list' =&gt; $list
    ]);

    return $this -&gt; fetch();
}

/**
 * @description 删除文章
 */
public function delArticle($id){
    $articleModel = new \app\admin\model\Article;
    $articleModel -&gt; delArticl([
        'id'  =&gt; $id,
    ]);
}

}

 页面中点击删除按钮拿到文章id调用ajax请求该控制器,当拿到code为1时代表删除成功,重载页面即可


更新文章

  • 更新文章模型方法
  • 更新文章页面
  • 更新文章控制器的操作方法

具体操作步骤:在列表页对应的文章后面点击“编辑”按钮,打开“编辑页”,先获取当前文章信息,并渲染页面,重新修改之后,点击“保存按钮”完成修改操作

 更新文章的模型方法

<?php
namespace app\admin\model;

use think\Model;

class Article extends Model {

/**
 * @description 修改文章
 * @param String $id id
 * @param String $title 标题
 * @param String $desc 描述
 * @param String $content 内容
 * @param String $status 状态
 * */
public function editArticl($id,$title,$desc,$content,$status){
    $article = $this -&gt; get($id);

    if($title &amp;&amp; $desc &amp;&amp; $content &amp;&amp; $status){
        $article-&gt;title = $title;
        $article-&gt;desc = $desc;
        $article-&gt;content = $content;
        $article-&gt;status = $status;

        $article-&gt;save();
        return $article;
    }
    
}

/**
 * @description 查询文章详情
 * @param String $id id
 * */
public function getArticleById($id){
    $res = $this-&gt;where('id',$id)-&gt;find();
    return $res;
}

}

 在文章编辑页面点击保存之后使用ajax将新的文章信息传给当前控制器的操作方法,通过在操作方法中调用模型方法来修改数据库

<?php

namespace app\admin\controller;

class Article extends Base {

/**
 * @description 编辑文章控制器
 * */
public function edit($id){
    // 查询文章
    $article = $this -&gt; findArticleById($id);

    // 获取post参数
    $title = $this-&gt;request-&gt;post('title');
    $desc = $this-&gt;request-&gt;post('desc');
    $status = $this-&gt;request-&gt;post('status');
    $content = $this-&gt;request-&gt;post('content');
    if($title &amp;&amp; $desc &amp;&amp; $status &amp;&amp; $content){
        $this -&gt; updateArticle($id,$title,$desc,$content,$status);
        $res = ['code' =&gt; 1 , 'msg' =&gt; '修改成功' ];
        return $res;
    }

    $this -&gt; assign([
        'title' =&gt; $article['title'],
        'desc' =&gt; $article['desc'],
        'status' =&gt; $article['status'],
        'content' =&gt; $article['content']
    ]);

    return $this -&gt; fetch();
}


/**
 * @description 修改文章
 */
public function updateArticle($id,$title,$desc,$content,$status){
    $articleModel = new \app\admin\model\Article;
    $articleModel -&gt; editArticl($id,$title,$desc,$content,$status);
}

/**
 * @description 根据id查询文章
 */
public function findArticleById($id){
    $articleModel = new \app\admin\model\Article;
    $article = $articleModel -&gt; getArticleById($id);
    return $article;
}

}

 


个人博客?:点此进入(http://xueshuai.top)

PHP学习交流群?:PHP学习交流群

前端学习交流群?:前端交流群

微信公众号?:叮当Ding