First, the Controller logic processing
Public function Add () {
if (is_post) {
$model =d (' Goods ');
if ($model->create (I (' post. '), 1)) {
if ($model->add ()) {
$this->success (' Add Success ', U (' added '));
Exit
}
}
$error = $model->geterror ();
$this->error ($error);
}
$this->show ();
}
Two models are responsible for the data section
Knowledge One: Data validation reference manual: Http://document.thinkphp.cn/manual_3_2.html#auto_validate
Knowledge two production thumbnail: http://document.thinkphp.cn/manual_3_2.html#image
Fields that are allowed to be received when the Create method is called when adding
Protected $insertFields =array (' goods_name ', ' price ', ' goods_desc ', ' Is_on_sale ');
Defines the rules for form validation, with the Create method in the controller
Protected $_validate=array (
Array (' Goods_name ', ' require ', ' product name cannot be empty ', 1),
Array (' Goods_name ', ' 1,45 ', ' product name is 1 to 45 characters ', 1, ' length '),
Array (' Price ', ' currency ', ' prices must be in currency format ', 1),
Array (' Is_on_sale ', ' 0,1 ', ' whether the shelves can only be 0, 12 values ', 1, ' in '),
);
protected function _before_insert (& $data, $option) {
$data [' Addtime ']=time ();
if (isset $_files[' logo ') && $_files[' logo ' [' error '] = = 0) {
$rootPath = C (' Img_rootpath ');
$upload = new \think\upload (Array (
' RootPath ' = $rootPath,
));//instantiation of the upload class
$upload->maxsize = (int) C (' img_maxsize ') * 1024 * 1024;//set attachment upload size
$upload->exts = C (' img_exts ');//Set attachment upload type
$upload->rootpath = $rootPath; Set Attachments upload root directory
$upload->savepath = ' goods/'; Name of the picture level two directory
Uploading files
$info = $upload->upload ();
if (! $info)
{
The error message of the upload failure is stored in the model, and the controller finally obtains the error message and displays
$this->error = $upload->geterror ();
return FALSE; Back to Controller
}
Else
{
$logoName = $info [' logo '] [' Savepath ']. $info [' logo '] [' savename '];
Spell out the file name of the thumbnail
$smLogoName = $info [' logo '] [' Savepath ']. ' Thumb_ '. $info [' logo '] [' savename '];
Generate thumbnail images
$image = new \think\image ();
Open the picture you want to work with
$image->open ($rootPath. $logoName);
$image->thumb (->save) ($rootPath. $smLogoName);
Place the form of a picture in a form
$data [' logo '] = $logoName;
$data [' sm_logo '] = $smLogoName;
}
}
}
Thinkphp adding operations (Controller to model logic)