Blame view

common/models/Orders.php 3.2 KB
30c37ebf   Administrator   Removed folder fr...
1
2
3
4
5
  <?php
  
  namespace common\models;
  
  use Yii;
2e3a817e   Administrator   24.03.16 finish 1
6
  use yii\behaviors\TimestampBehavior;
30c37ebf   Administrator   Removed folder fr...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /**
   * This is the model class for table "orders".
   *
   * @property integer $order_id
   * @property integer $customer_id
   * @property string $name
   * @property string $email
   * @property string $phone
   * @property integer $delivery
   * @property integer $payment
   * @property string $code
   * @property integer $status
   * @property integer $created_at
   * @property integer $updated_at
   *
   * @property OrderItems[] $orderItems
   */
  class Orders extends \yii\db\ActiveRecord
  {
290fae5b   Administrator   25.03.16 finish 1
26
27
  
  
30c37ebf   Administrator   Removed folder fr...
28
29
30
31
32
33
34
35
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'orders';
      }
  
2e3a817e   Administrator   24.03.16 finish 1
36
  
30c37ebf   Administrator   Removed folder fr...
37
38
39
40
41
42
43
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['customer_id', 'delivery', 'payment', 'status', 'created_at', 'updated_at'], 'integer'],
290fae5b   Administrator   25.03.16 finish 1
44
              [['created', 'updated'],'safe'],
2e3a817e   Administrator   24.03.16 finish 1
45
              [['name', 'email', 'phone'], 'required'],
30c37ebf   Administrator   Removed folder fr...
46
47
48
49
50
51
52
53
              [['name', 'email', 'code'], 'string', 'max' => 255],
              [['phone'], 'string', 'max' => 32],
          ];
      }
  
      /**
       * @inheritdoc
       */
2e3a817e   Administrator   24.03.16 finish 1
54
55
56
57
58
59
60
      public function behaviors()
      {
          return [
              TimestampBehavior::className(),
          ];
      }
  
290fae5b   Administrator   25.03.16 finish 1
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  
      public function getCreated(){
          if(!empty($this->created_at)){
              return date("Y-m-d" , $this->created_at);
          } else {
              return '';
          }
      }
  
  
      public function getUpdated(){
          if(!empty($this->updated_at)){
              return date("Y-m-d" , $this->updated_at);
          } else {
              return '';
          }
      }
  
2e3a817e   Administrator   24.03.16 finish 1
79
80
81
      /**
       * @inheritdoc
       */
30c37ebf   Administrator   Removed folder fr...
82
83
84
      public function attributeLabels()
      {
          return [
7b08eb78   Administrator   15.04.16 seo widget
85
86
87
88
89
90
91
92
93
94
95
              'order_id' => Yii::t('app', 'order_id'),
              'customer_id' => Yii::t('app', 'customer_id'),
              'name' => Yii::t('app', 'name'),
              'email' => Yii::t('app', 'email'),
              'phone' => Yii::t('app', 'phone'),
              'delivery' => Yii::t('app', 'delivery'),
              'payment' => Yii::t('app', 'payment'),
              'code' => Yii::t('app', 'code'),
              'status' => Yii::t('app', 'status'),
              'created_at' => Yii::t('app', 'created_at'),
              'updated_at' => Yii::t('app', 'updated_at'),
30c37ebf   Administrator   Removed folder fr...
96
97
98
          ];
      }
  
a4e099fb   Administrator   16.05.16 ann foto...
99
  
b44453de   Administrator   16.05.16 ann foto...
100
      public function beforeSave($insert)
a4e099fb   Administrator   16.05.16 ann foto...
101
      {
b44453de   Administrator   16.05.16 ann foto...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
          if (parent::beforeSave($insert)) {
              if($this->isNewRecord){
                  $customer = Customers::find()->where(['phone'=> $this->phone])->one();
  
                  if($customer instanceof Customers){
                      $this->customer_id = $customer->id;
                  } else {
                      $customer = new Customers(['scenario' => Customers::SCENARIO_ORDER]);
                      $customer->email = $this->email;
                      $customer->phone = $this->phone;
                      $customer->username = $this->name;
                      $customer->save();
                      $this->customer_id = $customer->id;
                  }
a4e099fb   Administrator   16.05.16 ann foto...
116
              }
b44453de   Administrator   16.05.16 ann foto...
117
118
119
              return true;
          } else {
              return false;
a4e099fb   Administrator   16.05.16 ann foto...
120
121
122
          }
  
  
b44453de   Administrator   16.05.16 ann foto...
123
124
  
  
a4e099fb   Administrator   16.05.16 ann foto...
125
126
      }
  
30c37ebf   Administrator   Removed folder fr...
127
128
129
130
131
132
133
134
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getOrderItems()
      {
          return $this->hasMany(OrderItems::className(), ['order_id' => 'order_id']);
      }
  }