diff --git a/src/behaviors/UploadedFileBehavior.php b/src/behaviors/UploadedFileBehavior.php new file mode 100644 index 0000000..4049a48 --- /dev/null +++ b/src/behaviors/UploadedFileBehavior.php @@ -0,0 +1,37 @@ +attributes)) { + $this->attributes = [ + BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->fileAttribute, + ]; + } + } + + protected function getValue($event) + { + if ($this->value === null) { + return UploadedFile::getInstance($this->owner, $this->fileAttribute); + } + + return parent::getValue($event); + } + +} diff --git a/src/components/ActiveRecordCollection.php b/src/components/ActiveRecordCollection.php new file mode 100644 index 0000000..6ec53a5 --- /dev/null +++ b/src/components/ActiveRecordCollection.php @@ -0,0 +1,16 @@ + "text", 'value' => '( NOT SET )' ]) + { + return ArrayHelper::parseAttribute($this, $attribute, $defalts); + } + + public function getValue($attribute, $defalt = '( NOT SET )') + { + return $this->parseAttribute($attribute, ['value' => $defalt])->value; + } + +} \ No newline at end of file diff --git a/src/db/Rankable.php b/src/db/Rankable.php new file mode 100644 index 0000000..ffaf500 --- /dev/null +++ b/src/db/Rankable.php @@ -0,0 +1,71 @@ +rankGoup ) { + $query->where([$this->rankGroup => $this->{$this->rankGroup}]); + } + + return $query->count(); + } + + public function changeRank( $new_rank ) + { + $rank_filed = $this->rankField; + $group = $this->rankGoup; + $old_rank = $this->{ $rank_filed }; + + if ( $new_rank == $old_rank ) { + return true; + } + + $value = ''; + + if ( $new_rank > $old_rank ) { + $value = $rank_filed . ' - 1'; + $where = $rank_filed . '>= :rank1 AND ' . $rank_filed . ' <= :rank2'; + } else { + $value = $rank_filed . ' + 1'; + $where = $rank_filed . ' <= :rank1 AND ' . $rank_filed . ' >= :rank2'; + } + + if ( $group ) { + $where .= ' AND ' . $group . ' = ' . $this->{ $group }; + } + + $query = Yii::$app->db->createCommand() + ->update( self::tableName(), [ $rank_filed => new Expression( $value ) ], $where ) + ->bindValue( ':rank1', $old_rank ) + ->bindValue( ':rank2', $new_rank ) + ->execute(); + + $this->updateAttributes( [ 'rank' => $new_rank ] ); + + return true; + } + +}