Explanation of the difference between instance Model methods in ThinkPHP

  • 2020-03-31 21:06:54
  • OfStack

In TP, we can use the following two methods to create a mapping object for the data table (which I will use for the moment)
First: $Test = D('Test')
Type 2: $Test = new Model('Test')
Although the two can be to select data, insert, delete, udpate operation, are quite different in the data validation,
So let's see what this looks like. Let's create a TestModel
 
class TestModel extends Model{ 
protected $_validate = array{ 
array('title','require',' Please enter a title ',1), 
array('content','require',' Please enter the content ',1), 
} 
} 

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

When running, you will find that, in the first way, an instance of a model will have a data check function, if the title is not filled in will prompt "please enter a title" (this is an automatic verification function provided by tp, of course, also need to define the verification conditions in the corresponding model); If you use the second one, there will be no

Related articles: