Thinkphp adding operations (Controller to model logic)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.