内容简介:修改数据库表前缀在编译器中查看已经连接好的数据库
前期准备
修改数据库表前缀
// 数据库表前缀
'prefix' => 'ming_',
创建相关数据表
在编译器中查看已经连接好的数据库
模型定义
首先数据库的表为ming_table1
定义模型为Table1
定义模型
<?php
namespace app\index\model;
use think\Model;
class Table1 extends Model
{
}
书写查询
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
try {
(new \app\index\model\Table1)->where('uid', 0)->select();
} catch (DataNotFoundException $e) {
} catch (ModelNotFoundException $e) {
} catch (DbException $e) {
}
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
模型字段
更改主键
<?php
namespace app\index\model;
use think\Model;
class Table1 extends Model
{
protected $pk = 'uid';
}
获取主键为1的
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
$Table1 = Table1::find(1);
var_dump($Table1);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
模型操作
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
$Table1 = Table1::select();
var_dump($Table1);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
查询
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
$Table1 = Table1::find(1);
var_dump($Table1['ming1']);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
赋值
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
$table1 = new Table1();
$table1->ming1 = "354mi";
$table1->ming2 = "eeee";
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
此时可以进行修改
添加数据
<?php
namespace app\index\controller;
use app\index\model\Table1;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return array|\think\Model
*/
public function index()
{
$table = new Table1();
$table->ming1 = "ming";
$table->ming2 = "ming3";
$table->uid = 34;
$table->save();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'not fond 404';
}
}
没有添加自增主键,所以需要添加
更新
剩下的几乎和hibernate一模一样
获取器
和mybatis中的typehadler一样
重写getStatusAttr方法
修改器
修改器同样等价于mybatis中的枚举类型
搜索器
封装查询
<?php
namespace app\model;
use think\Model;
class User extends Model
{
public function searchNameAttr($query, $value, $data)
{
$query->where('name','like', $value . '%');
}
public function searchCreateTimeAttr($query, $value, $data)
{
$query->whereBetweenTime('create_time', $value[0], $value[1]);
}
}
数据集
类似与 java 中的迭代器
自动时间戳
这个过
只读字段
只读,不删除
软删除
不是真正的删除
类型转换
模型输出
直接赋值给模板变量
模型事件
模型关联
等价于级联
以上所述就是小编给大家介绍的《thinkphp 模型》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 能量视角下的GAN模型(三):生成模型=能量模型
- 提高模型准确率:组合模型
- JVM内存模型 与 JMM内存模型
- Golang并发模型:轻松入门流水线模型
- C++11 中的内存模型(上):内存模型基础
- 机器学习中的判别式模型和生成式模型
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。