示例#1
0
 public function actionCreate()
 {
     $model = new Subdistrict();
     if (isset($_POST['Subdistrict'])) {
         $model->setAttributes($_POST['Subdistrict']);
         if ($model->save()) {
             if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                 Yii::app()->end();
             } else {
                 $this->redirect(array('view', 'id' => $model->id));
             }
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionUpload()
 {
     parent::actionUpload();
     $folder = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->request->baseUrl . '/upload/';
     // folder for uploaded files
     $file = $folder . basename($_FILES['uploadfile']['name']);
     if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
         $row = 0;
         if (($handle = fopen($file, "r")) !== FALSE) {
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 if ($row > 0) {
                     $model = Subdistrict::model()->findByPk((int) $data[0]);
                     if ($model === null) {
                         $model = new Subdistrict();
                     }
                     $model->subdistrictid = (int) $data[0];
                     $city = City::model()->findbyattributes(array('cityname' => $data[1]));
                     if ($city !== null) {
                         $model->cityid = $city->cityid;
                     }
                     $model->subdistrictname = $data[2];
                     $model->zipcode = $data[3];
                     $model->recordstatus = (int) $data[4];
                     try {
                         if (!$model->save()) {
                             $this->messages = $this->messages . Catalogsys::model()->getcatalog(' upload error at ' . $data[0]);
                         }
                     } catch (Exception $e) {
                         $this->messages = $this->messages . $e->getMessage();
                     }
                 }
                 $row++;
             }
         } else {
             $this->messages = $this->messages . ' memory or harddisk full';
         }
         fclose($handle);
     } else {
         $this->messages = $this->messages . ' check your directory permission';
     }
     if ($this->messages == '') {
         $this->messages = 'success';
     }
     echo $this->messages;
 }