前面我们学习的db类和sql类都属于模型的范畴,接下来我们要封装和使用model类
model类所有位置
model类的代码非常简单
<?php
namespace fastphp\base;
use fastphp\db\Sql;
class Model extends Sql
{
public function __construct($table)
{
parent::__construct($table);
}
}
model类继承了sql类,它具有sql类所有的功能,其它的功能暂时没有实现。
如果你觉得model类是多余的,可以重新构建你的项目。
接下来,我们在用户定义的控制器类中使用model类。
<?php
namespace app\controllers;
use fastphp\base\Controller;
use fastphp\base\Model;
class IndexController extends Controller
{
public function index(){
$model = new Model('student');
$data = $model->fields('no,name')->order('no')->fetchAll();
print_r($data);
}
public function add()
{
$data = ['no'=>'1835050','name'=>'赵六','sex'=>'男','age'=>20];
$model = new Model('student');
echo $model->add($data);
}
public function update()
{
$model = new Model('student');
echo $model->where('no=1835050')->update(['age'=>21]);
}
public function delete($no){
$model = new Model('student');
echo $model->where("no=$no")->delete();
}
}
运行一下代码,测试能否正常执行
http://127.0.0.1/mvc/public/index.php/index/add
http://127.0.0.1/mvc/public/index.php/index/update
http://127.0.0.1/mvc/public/index.php/index/index
http://127.0.0.1/mvc/public/index.php/index/delete?no=1835050