Car.php
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace common\models;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "car".
* @property integer $car_id
* @property string $model
* @property integer $year
* @property string $image
* @property string $color
* @property double $mileage
* @property integer $build
* @property string $option
* @property double $price
*/
class Car extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'car';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'model',
'year',
],
'required',
],
[
[ 'year' ],
'integer',
],
[
[
'mileage',
'price',
],
'number',
],
[
[
'option',
'build',
],
'string',
],
[
[
'model',
'color',
],
'string',
'max' => 255,
],
[
[ 'image' ],
'safe',
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'car_id' => Yii::t('app', 'Ідентификатор'),
'model' => Yii::t('app', 'Модель'),
'year' => Yii::t('app', 'Год производства'),
'image' => Yii::t('app', 'Изображение'),
'color' => Yii::t('app', 'Цвет'),
'mileage' => Yii::t('app', 'Пробег'),
'build' => Yii::t('app', 'Дата выпуска'),
'option' => Yii::t('app', 'Доп. опции'),
'price' => Yii::t('app', 'Цена'),
];
}
public function upload()
{
/**
* @var UploadedFile $image
*/
$this->image = UploadedFile::getInstance($this, 'image');
$image = $this->image;
if(!empty( $image )) {
$fullName = $image->baseName . time() . '.' . $image->extension;
$path = Yii::getAlias('@storage') . '/' . $fullName;
$image->saveAs($path);
$image->name = '/storage/' . $fullName;
} else {
$this->image = $this->getOldAttribute('image');
}
return true;
}
}