/** * Creates a new Goods model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Goods(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->goods_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * 把商品插入到数据库 * */ public static function insertGoods($goods) { $good = array(); foreach ($goods as $k => $v) { if ($v != null) { if ($k != '_csrf') { //判断促销日期是否存在 if ($k == 'sale_date') { //把日期变成时间戳 $good[$k] = strtotime($v); } else { $good[$k] = "{$v}"; } } } } $good['addtime'] = time(); $model = new Goods(); $model->setAttributes($good); return $model->save(); }
public function readfile() { $filename = 'GoodsFile/GoodsPrice.txt'; $readfile = fopen($filename, 'r'); while ($str = fgets($readfile)) { $items = explode(';', $str); echo iconv('UTF-8', 'CP1251', $items[2]) . '<br>'; $tp = Typeprice::find()->where(['type_price_name' => $items[5]])->one(); if (!isset($tp)) { continue; } $good = Goods::findOne(['good_1c_id' => $items[1]]); if (isset($good)) { echo $good->good_id . '<br>'; } else { echo iconv('UTF-8', 'CP1251', 'создаем новый объект ') . $items[1] . '<br>'; $good = new Goods(); } $good->good_1c_id = $items[1]; $good->good_name = $items[2]; $good->good_description = $items[4]; $good->good_price = $items[6] * 100; $good->typeprices_id = $tp->type_price_id; $good->save(); } fclose($readfile); }