控制层IndexController
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ //$food = M(‘food‘); //两个表联合查询 //var_dump($food->field(‘a.food_id,b.user_id‘)->table(‘__FOOD__ a, __PERSON__ b‘)->select()); $food =D(‘Food‘); var_dump($food->sql_one()->sql_two()->select()); $this->display(); } //控制器create()方法自动调用验证 //如果是直接 POST 过来的,直接使用 create()方法即可(假装下面是post过来的) public function login(){ $person = D(‘Person‘); $data[‘name‘] = ‘11‘; $data[‘password‘] = ‘1‘; $data[‘repassword‘] = ‘2‘; if ($person->create($data)) { echo ‘所有数据验证成功!‘; } else { var_dump($person->getError()); } } }
PersonModel层
<?php namespace Home\Model; use Think\Model; class PersonModel extends Model { //批量验证 protected $patchValidate = true; //自动验证内容 protected $_validate = array( array(‘name‘, ‘require‘, ‘用户名不得为空!‘, 0, ‘regex‘,3), //array(‘name‘, ‘email‘, ‘邮箱格式不正确!‘), //array(‘name‘, ‘url‘, ‘URL格式不合法!‘), //array(‘name‘, ‘currency‘, ‘货币格式不正确!‘), //array(‘name‘, ‘zip‘, ‘邮政编码必须是六位正整数!‘), //array(‘name‘, ‘number‘, ‘必须是正整数!‘), //array(‘name‘, ‘integer‘, ‘必须是整数!‘), //array(‘name‘, ‘double‘, ‘必须是浮点数!‘), //array(‘name‘, ‘english‘, ‘必须是纯英文!‘), //array(‘name‘, ‘/^\d{3,6}$/‘, ‘必须是3-6位纯数字‘, 0, ‘regex‘), //array(‘name‘, ‘李炎恢‘, ‘传递过来的值不相等‘, 0, ‘equal‘), //array(‘name‘, ‘李炎恢‘, ‘传递过来的必须不相等‘, 0, ‘notequal‘), array(‘repassword‘,‘password‘, ‘两个值不相等‘, 0, ‘confirm‘), array(‘name‘, ‘checkLength‘, ‘用户名必须在 3-5 位‘, 0, ‘callback‘, 3, array(3,5)), ); //自定义验证方法 protected function checkLength($str,$min,$max) { preg_match_all("/./", $str, $matches); $len = count($matches[0]); if ($len < $min || $len > $max) { return false; } else { return true; } } //将sql语句封装在模型定义类里 protected $_scope =array( ‘sql_one‘=>array( ‘where‘=>array(‘food_id‘=>array(‘GT‘,3)), ), ‘sql_two‘=>array( ‘order‘=>array(‘unit_price‘=>‘DESC‘), ‘limit‘=>5, ), ); //函数创建一个新的对象 PHP自带的函数 public function __construct(){ parent::__construct(); var_dump(‘\Home‘); } }
时间: 2024-11-08 09:52:36