public static function createDefault(IUser $user)
 {
     $currenciesItems = [['name' => 'Dollars', 'sign' => '$']];
     $currencies = [];
     foreach ($currenciesItems as $currencyItem) {
         $currency = new Currency();
         $currency->name = $currencyItem['name'];
         $currency->sign = $currencyItem['sign'];
         $currency->setUser($user);
         if ($currency->save()) {
             $currencies[] = $currency;
         }
     }
     return $currencies;
 }
 public function currencyValidator($attr)
 {
     if (!$this->hasErrors($attr)) {
         $this->_currency = Currency::find()->active()->andWhere(['id' => $this->{$attr}])->limit(1)->one();
         if (!$this->_currency) {
             $this->addError($attr, 'There is no such currency');
             return false;
         }
         $user = $this->_currency->user;
         if (!$user) {
             $this->addError($attr, 'There is no such currency');
             return false;
         }
         if (!$this->_user->isInSameGroup($user)) {
             $this->addError($attr, 'This currency belongs to another User');
             return false;
         }
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCurrency()
 {
     return $this->hasOne(Currency::className(), ['id' => 'currency_id']);
 }
 public function actionIndex()
 {
     $currency = Currency::find()->active()->forUsersInSameGroup($this->_user)->expand(Yii::$app->request->getQueryParams())->asArray()->all();
     return ['status' => true, 'data' => $currency];
 }
Exemplo n.º 5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCurrencies()
 {
     return $this->hasMany(Currency::className(), ['user_id' => 'id']);
 }