ThinkPHP中实例Model方法的区别说明_PHP

WBOY
Release: 2016-06-01 12:18:10
Original
856 people have browsed it

ThinkPHP

在TP中,我们可以用下面两种方法去创建一个数据表的映射对象(我暂时用到的)
第一种:$Test = D('Test')
第二种:$Test = new Model('Test')
虽然这两种都可以对数据进行select,insert,delete,udpate操作,在数据验证上有很大的不同,
我们来看看效果,先创建一个 TestModel
复制代码 代码如下:
class TestModel extends Model{
protected $_validate = array{
array('title','require','请输入标题',1),
array('content','require','请输入内容',1),
}
}

创建一个TestAction
复制代码 代码如下:
class TestAction extends Action{
public function Dtest(){
$test = D('Test'); //第一种情况
$test = new Model('Test'); //第二种情况
if($test->Create()){
$test->Add();
}else{
$test->getError();
}
}
}

在运行的时候,大家会发现,用第一种方式实例一个模型就会有数据检查功能,如果 title 没有填写的话就会提示 “请输入标题” (这个是tp提供的一个自动验证功能,当然也需要在相应的model中定义好验证条件);如果用第二种就没有了·····
Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!