alias('wz') ->leftJoin(['w' => Warehouse::tableName()], 'w.id = wz.warehouse_id') ->where(['wz.is_delete' => 0, 'wz.store_id' => $this->store_id]); if (!empty($this->name)) { $query->andWhere(['like', 'wz.name', $this->name]); } if (!empty($this->warehouse_id)) { $query->andWhere(['wz.warehouse_id' => $this->warehouse_id]); } $query->orderBy('wz.sort DESC')->select('wz.*,w.name as warehouse_name'); $pagination = pagination_make($query); return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function save () { $t = \Yii::$app->db->beginTransaction(); try { $model = WarehouseZone::findOne(['id' => $this->id, 'store_id' => $this->store_id]); if (empty($model)) { $model = new WarehouseZone(); $model->store_id = $this->store_id; } $model->name = $this->name; $model->warehouse_id = $this->warehouse_id; $model->sort = $this->sort; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function del () { try { if ($this->id) { WarehouseZone::updateAll(['is_delete' => 1], ['id' => $this->id]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }