refactor: rewrite everything with params to not suck ass

This commit is contained in:
b1ek 2024-08-20 23:35:42 +10:00
parent d3a4577913
commit b85dd5ff13
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 14 additions and 42 deletions

View File

@ -18,8 +18,8 @@ class m240819_102301_image extends Migration
'sha256' => Schema::TYPE_STRING . ' NOT NULL',
'original_name' => Schema::TYPE_STRING . ' NOT NULL'
]);
$this->addColumn('parameters', 'icon', 'INT(11) DEFAULT NULL');
$this->addColumn('parameters', 'icon_gray', 'INT(11) DEFAULT NULL');
$this->addColumn('parameters', 'icon_id', 'INT(11) DEFAULT NULL');
$this->addColumn('parameters', 'icon_gray_id', 'INT(11) DEFAULT NULL');
}
/**
@ -28,8 +28,8 @@ class m240819_102301_image extends Migration
public function safeDown()
{
$this->dropTable('image');
$this->dropColumn('parameters', 'icon');
$this->dropColumn('parameters', 'icon_gray');
$this->dropColumn('parameters', 'icon_id');
$this->dropColumn('parameters', 'icon_gray_id');
}
/*

View File

@ -24,27 +24,12 @@ class Parameter extends \yii\db\ActiveRecord
public function getIcon()
{
return $this->hasOne(Image::class, [ 'id' => 'icon' ]);
return $this->hasOne(Image::class, [ 'id' => 'icon_id' ]);
}
public function getIconGray()
{
return $this->hasOne(Image::class, [ 'id' => 'icon_gray' ]);
}
public function fields()
{
$fields = parent::fields();
$fields['icon'] = function (Parameter $parameter) {
return $parameter->relatedRecords['icon'];
};
$fields['icon_gray'] = function (Parameter $parameter) {
return $parameter->relatedRecords['iconGray'];
};
return $fields;
return $this->hasOne(Image::class, [ 'id' => 'icon_gray_id' ]);
}
/**

View File

@ -55,28 +55,15 @@ class ParameterForm extends Model
if ($param->type == 2) {
if ($this->icon) {
$icon = Image::fromUploadedFile($this->icon);
// https://rent4health.com/wp-content/uploads/2020/02/Under-arm-crutches-1.png
if ($param->getAttribute('icon') !== null) {
$param->icon = $icon->id;
} else {
$param->setAttribute('icon', $icon->id);
}
$param->icon_id = $icon->id;
}
if ($this->iconGray) {
$iconGray = Image::fromUploadedFile($this->iconGray);
if ($param->getAttribute('icon_gray') !== null) {
$param->iconGray = $iconGray->id;
} else {
$param->setAttribute('icon_gray', $iconGray->id);
}
$param->icon_gray_id = $iconGray->id;
}
$param->setAttributes([
'icon' => $param->icon,
'iconGray' => $param->iconGray,
]);
} else {
$param->icon_id = null;
$param->icon_gray_id = null;
}
$param->setAttributes([

View File

@ -24,7 +24,7 @@ class ParameterSearch extends Parameter
public function search($params): ActiveDataProvider
{
$query = Parameter::find()->with(['icon', 'iconGray']);
$query = Parameter::find();//->with(['icon', 'iconGray']);
$dataProvider = new ActiveDataProvider([
'query' => $query,

View File

@ -31,7 +31,7 @@ $this->title = 'params list';
'label' => 'Icon',
'value' => function (Parameter $model) {
if ($model->icon) {
$url = '/api/image?sha256=' . $model->getRelatedRecords()['icon']->sha256;
$url = '/api/image?sha256=' . $model->icon->sha256;
return Html::a(Html::img($url, [ 'width' => 150, 'height' => 150 ]), $url, [ 'alt' => 'icon', 'target' => '_blank' ]);
}
},
@ -50,7 +50,7 @@ $this->title = 'params list';
'label' => 'Gray Icon',
'value' => function (Parameter $model) {
if ($model->iconGray) {
$url = '/api/image?sha256=' . $model->getRelatedRecords()['iconGray']->sha256;
$url = '/api/image?sha256=' . $model->iconGray->sha256;
return Html::a(Html::img($url, [ 'width' => 150, 'height' => 150 ]), $url, [ 'alt' => 'grayscale icon', 'target' => '_blank' ]);
}
},