Last active
January 31, 2018 11:45
-
-
Save onmotion/379d5dd6bf0a1aa2a1ff092f13b2989c to your computer and use it in GitHub Desktop.
Yii2 form model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace backend\models\forms; | |
use common\models\Badge; | |
use common\models\BadgeCountryAccess; | |
use Yii; | |
use yii\base\Model; | |
use yii\web\UploadedFile; | |
/** | |
* Class BadgeForm | |
* @package backend\models\forms | |
* @property Badge $badge | |
* @property BadgeCountryAccess[] $countriesAccess | |
*/ | |
class BadgeForm extends Model | |
{ | |
public $countriesAccess; | |
public function rules() | |
{ | |
return [ | |
[['Badge'], 'required'], | |
[['BadgeCountryAccess'], 'safe'], | |
]; | |
} | |
public function beforeValidate() | |
{ | |
$imageInstance = UploadedFile::getInstance($this->badge, 'imageFile'); | |
if ($imageInstance) { | |
$this->badge->imageFile = $imageInstance; | |
} | |
return parent::beforeValidate(); | |
} | |
public function afterValidate() | |
{ | |
$error = false; | |
if (!$this->badge->validate()) { | |
$error = true; | |
} | |
if (!Model::validateMultiple($this->countriesAccess)) { | |
$error = true; | |
} | |
if ($error) { | |
$this->addError(null); // add an empty error to prevent saving | |
} | |
parent::afterValidate(); | |
} | |
public function save() | |
{ | |
if (!$this->validate()) { | |
return false; | |
} | |
$transaction = Yii::$app->db->beginTransaction(); | |
if (!$this->badge->save()) { | |
$transaction->rollBack(); | |
return false; | |
} | |
$DA = []; | |
foreach ($this->countriesAccess as $country) { | |
$DA = array_merge($DA, $country->getDirtyAttributes()); | |
} | |
if (!empty($DA)) { | |
if ($this->countriesAccess[0]::deleteAll(['bca_badge_id' => $this->badge->badge_id]) === false) { | |
$transaction->rollBack(); | |
return false; | |
} | |
$rows = []; | |
foreach ($this->countriesAccess as $country) { | |
if ($country->is_available === true) { | |
$country->bca_badge_id = $this->badge->badge_id; | |
$rows[] = $country->getAttributes(null, ['is_available']); | |
} | |
} | |
if (!empty($rows)) { | |
if (!\Yii::$app->db->createCommand()->batchInsert($this->countriesAccess[0]::tableName(), array_keys($rows[0]), $rows)->execute()) { | |
$transaction->rollBack(); | |
return false; | |
} | |
} | |
} | |
$transaction->commit(); | |
return true; | |
} | |
public function setBadgeCountryAccess($val) | |
{ | |
if (empty($this->countriesAccess)) { | |
$this->countriesAccess = $val; | |
} else { | |
Model::loadMultiple($this->countriesAccess, $val, ''); | |
} | |
} | |
public function getBadgeCountryAccess() | |
{ | |
return $this->countriesAccess ?? []; | |
} | |
public function setBadge($val) | |
{ | |
if (empty($this->badge)) { | |
$this->badge = $val; | |
} else { | |
$this->badge->load($val, ''); | |
} | |
} | |
public function getBadge() | |
{ | |
return $this->badge ?? null; | |
} | |
/** | |
* override | |
* @param array $data | |
* @param null $formName | |
*/ | |
public function load($data, $formName = null) | |
{ | |
$this->setAttributes($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment