public function add()
 {
     if (IS_POST) {
         $model = new \Model\GoodsModel();
         if ($data = $model->create()) {
             if ($model->add($data)) {
                 $this->success('添加成功', U('showlist'), 1);
                 exit;
             } else {
                 $error = $model->getError();
                 //获取模型里面的error属性的值
                 if (empty($error)) {
                     $error = '添加失败';
                 }
                 $this->error($error);
             }
         } else {
             $this->error($model->getError());
         }
     }
     //取出商品栏目
     $cat_model = new \Model\CategoryModel();
     $cat_list = $cat_model->getTree();
     $this->assign('cat_list', $cat_list);
     //取出商品类别
     $type_list = M('type')->select();
     $this->assign('type_list', $type_list);
     $this->display();
 }
Exemple #2
0
 public function add()
 {
     $goods = new \Model\GoodsModel();
     if (!empty($_POST)) {
         //做商品图片的处理
         //var_dump($_FILES);die;
         if ($_FILES['goods_img']['error'] === 0) {
             $cfg = array('rootPath' => './Admin/Public/goods/');
             $up = new \Think\Upload($cfg);
             $z = $up->uploadOne($_FILES['goods_img']);
             $_POST['goods_big_img'] = $up->rootPath . $z['savepath'] . $z['savename'];
             $bigimg = $_POST['goods_big_img'];
             //对上传图片进行缩略图处理
             $im = new \Think\Image();
             //实例化对象
             $im->open($bigimg);
             //相对服务器路径打开图片
             $im->thumb(165, 165, 3);
             //自适应大小制作缩略图
             $smallimg = $up->rootPath . $z['savepath'] . "small_" . $z['savename'];
             $im->save($smallimg);
             //保存
             $_POST['goods_small_img'] = $smallimg;
             //var_dump($_POST['goods_big_img']);die;
         }
         //收集表单数据
         $info = $goods->create();
         $result = $goods->add($info);
         if ($result) {
             //跳转
             $this->redirect('showlist', array(), 2, '添加商品成功');
         }
     }
     $category = new \Model\CategoryModel();
     $cats = $category->select();
     $this->assign('cats', $cats);
     $this->display();
 }