目录
- 要实现的功能
- 思路:
- html里
- 控制器里
- 模型里的后置勾子
beforeUpdate()
要实现的功能
html的表单里提交过数据到控制器,控制器调用model
里的save()
方并过滤掉不需要的数据后保存到goods
表,
保存后会自动调用模型里的后置勾子afterInsert()
,后置勾子的回调$goods
里得到goods
表里新插入的主键后组装好数据,
再插入到number_price
表里
思路:
- 控制器里的用调用模型用
save()
方法保存,在模型里的用protected $field=true;
//当插入到当然模型对应表里不存在的字段时,就会被忽略掉 - 数据插入到goods表后就会自动执行模型里的
afterInsert()
方法 - 在
afterInsert()
方法里回调的$goods
里就会获取到插入goods
表里的数据,并获取到插入后的主键id - 在html表格里用
name="mp[{$ml.id}]"
带主键的方式提交,这样数据的字段名和字段值就能对应上了 - 新插入goods表里的商品主键id有了,三种会员的价格也有了,现在可以在
afterInsert()
里循环把这三种价格数据分别插入到tp_member_price
表里了
html里
<!-- 会员价格 ?--><div id="mbprice" class="tab-pane"> ???{volist name="mlRes" id="ml"} ???????<div class="form-group"> ???????????<label for="username" class="col-sm-2 control-label no-padding-right">{$ml.level_name}</label> ???????????<div class="col-sm-6"> ???????????????<input class="form-control" placeholder="" name="mp[{$ml.id}]" type="text"> ???????????</div> ???????????<p class="help-block col-sm-4 red">单位:元 精确到小数点后2位</p> ???????</div> ???{/volist}</div><!-- 给主图上批量生成三张缩略图 ?--><div id="goodsphoto" class="tab-pane"> ???<div class="form-group"> ???????<label for="username" class="col-sm-2 control-label no-padding-right"></label> ???????<div class="col-sm-6"> ???????????<a href="#" onclick="addrow(this);">[+]</a><input class="form-control" style="border:none; box-shadow:none; width:50%; display:inline-block;" name="goods_photo[]" type="file"> ???????</div> ???</div> ???<div id="goods_photo"></div></div>
控制器里
public function add(){ ???if(request()->isPost()){ ???????//接收所有的表单所有的post数据 ???????$data=input(‘post.‘); ???????// dump($data); die; ???????//验证post数据 ???????// dump($_FILES);die; ???????$validate = validate(‘goods‘); ???????if(!$validate->check($data)){ ???????????$this->error($validate->getError()); ???????} ???????//调用模型添加数据,添加之前会运行 Goods::beforeInsert方法 ???????//调用模型添加数据,添加之后会运行 Goods::afterInsert方法 ???????$add=model(‘goods‘)->save($data); ???????if($add){ ???????????$this->success(‘添加商品成功!‘,‘lst‘); ???????}else{ ???????????$this->error(‘添加商品失败!‘); ???????} ???????return; ???}}
模型里的后置勾子beforeUpdate()
protected static function init(){ ???//$goods获取到的是插入goods表里的数据,并获取到插入后的主键id ???Goods::afterInsert(function($goods){ ???????//接受表单数据 ???????$goodsData=input(‘post.‘); ???????????????// 批量写入会员价格 ???????$mpriceArr=$goods->mp; ???????$goodsId=$goods->id; ???????if($mpriceArr){ ???????????foreach ($mpriceArr as $k => $v) { ???????????????if(trim($v) == ‘‘){ ???????????????????continue; ???????????????}else{ ???????????????????db(‘member_price‘)->insert([‘mlevel_id‘=>$k,‘mprice‘=>$v,‘goods_id‘=>$goodsId]); ???????????????} ???????????} ???????} ???????????????// 商品相册处理 给主图上批量生成三张缩略图 ???????if($goods->_hasImgs($_FILES[‘goods_photo‘][‘tmp_name‘])){ ???????????$files = request()->file(‘goods_photo‘); ???????????foreach($files as $file){ ???????????????// 移动到框架应用根目录/public/uploads/ 目录下 ???????????????$info = $file->move(ROOT_PATH . ‘public‘ . DS .‘static‘. DS .‘uploads‘); ???????????????if($info){ ???????????????????// 输出 42a79759f284b767dfcb2a0197904287.jpg ???????????????????$photoName=$info->getFilename(); ???????????????????$ogphoto=date("Ymd"). DS . $photoName; ???????????????????$bigphoto=date("Ymd"). DS . ‘big_‘.$photoName; ???????????????????$midphoto=date("Ymd"). DS . ‘mid_‘.$photoName; ???????????????????$smphoto=date("Ymd"). DS . ‘sm_‘.$photoName; ???????????????????$image = \think\Image::open(IMG_UPLOADS.$ogphoto); ???????????????????$image->thumb(500, 500)->save(IMG_UPLOADS.$bigphoto); ???????????????????$image->thumb(200, 200)->save(IMG_UPLOADS.$midphoto); ???????????????????$image->thumb(80, 80)->save(IMG_UPLOADS.$smphoto); ???????????????????db(‘goods_photo‘)->insert([‘goods_id‘=>$goodsId,‘og_photo‘=>$ogphoto,‘big_photo‘=>$bigphoto,‘mid_photo‘=>$midphoto,‘sm_photo‘=>$smphoto]); ???????????????}else{ ???????????????????// 上传失败获取错误信息 ???????????????????echo $file->getError(); ???????????????} ???????????????} ???????} ???}}
判断$_FILES[‘goods_photo‘][‘tmp_name‘]
是否为空来判断是否有图片上传
第39课 thinkphp5完成商品会员价格功能(后置勾子afterInsert)
原文地址:https://www.cnblogs.com/haima/p/9841062.html