Add active record classes

This commit is contained in:
AdeAttwood 2017-09-17 09:46:45 +01:00
parent 6d2b1b569c
commit d56bcc42d7
5 changed files with 165 additions and 0 deletions

View file

@ -0,0 +1,37 @@
<?php
namespace yiiaddon\behaviors;
use yii\db\BaseActiveRecord;
use yii\web\UploadedFile;
class UploadedFileBehavior extends \yii\behaviors\AttributeBehavior
{
public $fileAttribute = 'file';
public $value;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->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);
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace yiiaddon\components;
use yiiaddon\helpers\ArrayHelper;
use yiiaddon\helpers\ArrayObject;
class ActiveRecordCollection extends \yiiaddon\helpers\ArrayObject
{
public function map($key, $value)
{
return new ArrayObject(ArrayHelper::map($this, $key, $value));
}
}

15
src/db/ActiveQuery.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace yiiaddon\db;
use yiiaddon\components\ActiveRecordCollection;
class ActiveQuery extends \yii\db\ActiveQuery
{
public function all($db = null)
{
return new ActiveRecordCollection(parent::all($db));
}
}

26
src/db/ActiveRecord.php Normal file
View file

@ -0,0 +1,26 @@
<?php
namespace yiiaddon\db;
use Yii;
use yiiaddon\helpers\ArrayHelper;
class ActiveRecord extends \yii\db\ActiveRecord
{
public static function find()
{
return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
}
public function parseAttribute($attribute, $defalts = [ 'format' => "text", 'value' => '( NOT SET )' ])
{
return ArrayHelper::parseAttribute($this, $attribute, $defalts);
}
public function getValue($attribute, $defalt = '( NOT SET )')
{
return $this->parseAttribute($attribute, ['value' => $defalt])->value;
}
}

71
src/db/Rankable.php Normal file
View file

@ -0,0 +1,71 @@
<?php
namespace yiiaddon\db;
use Yii;
use yii\db\Expression;
trait Rankable
{
public $rank_filed = 'rank';
public $rank_group = false;
public function getRankField()
{
return 'rank';
}
public function getRankGoup()
{
return false;
}
public function getNextRank()
{
$query = self::find();
if ( $this->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;
}
}