php namespace app modules shop models use Yii use yii base Model use y

 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
<?php
namespace app\modules\shop\models;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use app\modules\shop\models\Goods;
use app\modules\shop\models\Categories;
class AddGoods extends Model{
public $name;
public $description;
public $category;
public $price;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'description', 'price', 'category'], 'required'],
['name', 'string', 'length' => [2,30]],
['description' ,'string', 'length' => [5, 5000]],
['price', 'integer', 'min' => '1'],
['category', 'integer', 'min' => '1'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => \Yii::t('app', 'SHOP_NAME'),
'description' => \Yii::t('app', 'SHOP_DESCRIPTION'),
'category' => \Yii::t('app', 'SHOP_CATEGORY'),
'price' => \Yii::t('app', 'SHOP_PRICE'),
'dop_category' => \Yii::t('app', 'SHOP_DOP_CATEGORY'),
];
}
public function getCategories()
{
$cats = Categories::find()->where(['!=', 'parent', '0'])->select(['id','name', 'parent'])->orderBy('parent')->asArray()->all();
$cats_2 = ArrayHelper::map($cats, 'id', 'name', 'parent');
$cats_2 = ArrayHelper::htmlEncode($cats_2);
return $cats_2;
}
public function addGood()
{
if($this->validate())
{
$good = new Goods();
$good->name = $this->name;
$good->description = $this->description;
$good->category = $this->category;
$good->price = $this->price;
$good->user = \Yii::$app->user->getId();
$good->moderate = '0';
if($good->save())
return TRUE;
else
return FALSE;
}
}
}