/** * 定义与IProductBase的关联关系 */ public function getIproduct() { return $this->hasMany(IProductBase::className(), ['uid' => 'uid']); }
/** * 定义与product_base的关联关系 */ public function getProduct() { return $this->hasOne(IProductBase::className(), ['id' => 'product_id']); }
/** * 迁移函数 * * 迁移前,在迁移的表里面加两字段,迁移字段_new(eg:迁移字段path,加path_new字段)和modify_flag(默认值为0) * 迁移成功的数据对应modify_flag=1,失败如下: * -1001 路径为空 * -1002 该路径下不存在图片 * 迁移图片在frontend/web/uploads下 * 迁移到frontend/web/uploads_new下 * 迁移完后,确认无误后 uploads移除备份,uploads_new更名为uploads * *@params object $model 迁移表对应model对象 *@params string $attrName 迁移表的迁移字段名 *@params boolean $makeThumb 是否生成缩略图 *@params boolean inProductImg 是否入库ProductImg 表 * * @return 详情 */ public function actionModify($attrName, $makeThumb = true, $inProductImg = false, $pos = '', $type = '') { //迁移表对应model $model = new IProductBase(); $class = $model->className(); $attrFlag = $attrName . "_flag"; $attrNew = $attrName . "_new"; //读取迁移表字段字段 $arr1 = $model = $class::find()->indexBy('id')->where([$attrFlag => 0])->asArray()->all(); // print_r($arr1);exit; foreach ($arr1 as $k => $v) { //取出图片路径 $url = $v[$attrName]; //$name为原图片在web下的最外层路径 $real = $this->getPath($url); // echo $real."\n"; if ($url !== '' && file_exists($real)) { //生成图片迁移路径 $normalPath = $this->newPath($url); $imagePath = str_replace('/uploads/', '/uploads_new/', $normalPath); file_exists(dirname($imagePath)) or mkdir(dirname($imagePath), 0777, true); if (copy($real, $imagePath)) { //是否生成缩略图 if ($makeThumb) { //生成缩略图迁移路径 $thumbPath = $this->newPath($url, false); $thumbPath = str_replace('/uploads/', '/uploads_new/', $thumbPath); try { $this->makeThumb($real, $thumbPath); } catch (\Exception $ex) { $model = $class::findOne(['id' => $k]); //标记入库是否成功 $model->{$attrFlag} = 1003; $model->save(); echo "-----异常------\n"; continue; } } //生成入库路径 $savePath = $this->savePath($normalPath); //更改后路径保存字段 $new = $attrNew; $model = $class::findOne(['id' => $k]); $model->{$new} = $savePath; //迁移成功标记 modify_flag为1 其默认值为0 $model->{$attrFlag} = 1; $model->save() ? $row1 = $k . "--入库1成功" : ($row1 = $k . "--入库1失败"); //是否入库ProductImg表 if ($inProductImg) { $mIProductImage = new IProductImage(); $mIProductImage->pos_id = $pos; $mIProductImage->product_id = $k; $mIProductImage->url = $savePath; $mIProductImage->type = $type; $mIProductImage->create_time = time(); $mIProductImage->update_time = time(); $mIProductImage->save() ? $row1 .= "--入库2成功---" : ($row1 .= "--入库2失败---"); } echo $row1 . "\n"; } } else { if (empty($url)) { $model = $class::findOne(['id' => $k]); //标记入库是否成功 $model->{$attrFlag} = 1001; $model->save(); echo $k . "--失败---此字段为空" . "\n"; } else { $model = $class::findOne(['id' => $k]); //标记入库是否成功 $model->{$attrFlag} = 1002; $model->save(); echo $k . "---失败--|" . $url . "路径下图片不存在" . "\n"; } } } echo "--------------sussful finshed\n"; $this->actionLog($attrName); }