Home  >  Article  >  Backend Development  >  Explanation of the differences between instance Model methods in ThinkPHP_PHP Tutorial

Explanation of the differences between instance Model methods in ThinkPHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:35:09724browse

In TP, we can use the following two methods to create a mapping object of a data table (what I use temporarily)
The first method: $Test = D('Test')
The second method: $ Test = new Model('Test')
Although both can perform select, insert, delete, and udpate operations on data, they are very different in data verification.
Let’s take a look at the effect. First Create a TestModel

Copy code The code is as follows:

class TestModel extends Model{
protected $_validate = array{
array('title','require','Please enter the title',1),
array('content','require','Please enter the content',1),
}
}

Create a TestAction
Copy the code The code is as follows:

class TestAction extends Action{
public function Dtest(){
$test = D('Test'); //First case
$test = new Model('Test'); //Second case
if($test->Create()){
$test->Add();
}else{
$test->getError();
}
}
}

When running, you will find that using the first method to instantiate a model will have a data check function. If the title is not filled in, it will prompt "Please enter a title. ” (This is an automatic verification function provided by tp. Of course, the verification conditions need to be defined in the corresponding model); if you use the second method, it will not be available...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322282.htmlTechArticleIn TP, we can use the following two methods to create a mapping object of a data table (I temporarily use ) The first one: $Test = D('Test') The second one: $Test = new Model('Test') Although this...
Statement:
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