插件数据库模型

优质
小牛编辑
141浏览
2023-12-01

创建插件自定义模型

在插件model目录下创建PluginDemoModel.php文件,内容如下:

<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\demo\model;//Demo插件英文名,改成你的插件英文就行了
use think\Model;

//Demo插件英文名,改成你的插件英文就行了
//插件数据表最好加个plugin前缀再加表名,这个类就是对应“表前缀+plugin_demo”表
class PluginDemoModel extends Model
{
    //自定义方法
    function test()
    {
        echo "hello";
    }
}

使用自定义模型

//先 use
use plugins\demo\model\PluginDemoModel;
//然后直接用PluginDemoModel

$pluginDemoModel = new PluginDemoModel();

在控制器中使用自定义模型

<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\demo\controller; //Demo插件英文名,改成你的插件英文就行了
use cmf\controller\PluginBaseController;
use plugins\Demo\Model\PluginDemoModel;
use think\Db;

class IndexController extends PluginBaseController
{

    function index($id)
    {

        $users = Db::name("user")->limit(0, 5)->select();
        $demos=PluginDemoModel::all();

       // print_r($demos);

        $this->assign("users", $users);

        return $this->fetch("/index");
    }

}