/** * Создание контента * @return string */ public function actionCreate() { $model = new Country(); $uploadImg = new UploadForm(); if (Yii::$app->request->isPost) { $uploadImg->img = UploadedFile::getInstance($uploadImg, 'img'); if ($uploadImg->img && $uploadImg->validate()) { $uploadImg->img->saveAs('uploads/icoflags/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension); } else { print_r($uploadImg->getErrors()); } } if ($model->load(Yii::$app->request->post())) { $model->name = Yii::$app->request->post('Country')['name']; $model->iso_code = Yii::$app->request->post('Country')['iso_code']; $model->soc_abrev = Yii::$app->request->post('Country')['soc_abrev']; $model->soccer_code = Yii::$app->request->post('Country')['soccer_code']; if (isset($uploadImg->img)) { $model->icon = Url::base() . 'uploads/icoflags/' . Yii::$app->translater->translit($uploadImg->img->baseName) . '.' . $uploadImg->img->extension; } $model->save(false); return $this->redirect(Url::toRoute('countries/index')); } else { return $this->render('_form', ['model' => $model, 'uploadImg' => $uploadImg]); } }
/** * Creates a new Country model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Country(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->country_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, ['name_en' => 'required', 'name_cn' => 'required', 'call_code' => 'required']); $model = new Country(); $model->name_en = $request->name_en; $model->name_cn = $request->name_cn; $model->short = $request->short; $model->call_code = $request->call_code; $model->active = $request->active ? 1 : 0; $model->save(); }
public function save($request) { $country = new Country(); $country->country_name = $request->input('country_name'); $country->country_code = $request->input('country_code'); $country->currency = $request->input('currency'); $country->status = $request->input('status'); if ($country->save()) { return $country->id; } else { return false; } }
/** * Updates an existing Country model. * If update is successful, the browser will be redirected to the 'update' page. * @param integer $id * @return mixed */ public function actionUpdate($id = null) { if ($id) { $model = $this->findModel($id); } else { $model = new Country(); } if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', __('Your changes have been saved successfully.')); return $this->redirect(['index']); } else { return $this->renderAjax('update', ['model' => $model]); } }
public function testCreateModel() { $indonesia = new Country(array('name' => 'Indonesia')); $indonesia->save(); $user = new User(array('fullname' => 'Mukhamad Ikhsan', 'email' => '*****@*****.**', 'password' => 'rahasia', 'profile' => array('profession' => 'programmer', 'nationality' => $indonesia))); $this->assertEqual('Mukhamad Ikhsan', $user->fullname); $this->assertEqual('programmer', $user->profile->profession); $this->assertEqual('Indonesia', $user->profile->nationality->name); $this->assertTrue($user->save()); $ikhsan = User::getRepository()->findOneBy(array('email' => '*****@*****.**')); $users = User::findAll(array('where' => array('and' => array(array('fullname' => array('like' => '%Ikhsan')), array('fullname' => array('eq' => 'Mukhamad Ikhsan')), array('fullname' => array('neq' => 'Haris Riswandi')))), 'leftJoin' => array(array('field' => 'groups')))); $compacts = User::getCompactList('fullname'); $this->assertEqual(1, count($users)); $this->assertEqual(1, count($compacts)); $this->assertEqual('Mukhamad Ikhsan', $ikhsan->fullname); $this->assertEqual('programmer', $ikhsan->profile->profession); $this->assertEqual('Indonesia', $ikhsan->profile->nationality->name); $ikhsan->delete(); $indonesia->delete(); $this->assertNull(User::getRepository()->findOneBy(array('email' => '*****@*****.**'))); $this->assertNull(Country::getRepository()->findOneBy(array('name' => 'Indonesia'))); }
public function actionGetCountriesFromYndex() { $url = "https://pogoda.yandex.ru/static/cities.xml"; $dom = new \DomDocument(); $dom->load($url); $countries = $dom->getElementsByTagName("country"); foreach ($countries as $country) { $newCountry = new Country(); $newCountry->name = $country->getAttribute('name'); $newCountry->save(false); } }