Commit 0d441cff06ff46f822e7fc57df39736909d32e8f

Authored by Alexey Boroda
1 parent 856de6bd

-Cabinet ready

migrations/m170512_092259_create_wishlist_table.php 0 → 100644
  1 +<?php
  2 +
  3 + use yii\db\Migration;
  4 +
  5 + /**
  6 + * Handles the creation of table `wishlist`.
  7 + */
  8 + class m170512_092259_create_wishlist_table extends Migration
  9 + {
  10 + /**
  11 + * @inheritdoc
  12 + */
  13 + public function up()
  14 + {
  15 + $this->createTable(
  16 + 'wishlist',
  17 + [
  18 + 'user_id' => $this->integer(),
  19 + 'product_id' => $this->integer(),
  20 + 'variant_id' => $this->integer(),
  21 + ]
  22 + );
  23 +
  24 + $this->addForeignKey('user_fk', 'wishlist', 'user_id', 'customer', 'id', 'CASCADE', 'CASCADE');
  25 + $this->addForeignKey('product_fk', 'wishlist', 'product_id', 'product', 'id', 'CASCADE', 'CASCADE');
  26 + $this->addForeignKey('variant_fk', 'wishlist', 'variant_id', 'variant', 'id', 'CASCADE', 'CASCADE');
  27 +
  28 + $this->createIndex(
  29 + 'user_variant_fk',
  30 + 'wishlist',
  31 + [
  32 + 'user_id',
  33 + 'variant_id',
  34 + ],
  35 + true
  36 + );
  37 + }
  38 +
  39 + /**
  40 + * @inheritdoc
  41 + */
  42 + public function down()
  43 + {
  44 + $this->dropIndex(
  45 + 'user_variant_fk',
  46 + 'wishlist'
  47 + );
  48 +
  49 + $this->dropForeignKey('variant_fk', 'wishlist');
  50 + $this->dropForeignKey('product_fk', 'wishlist');
  51 + $this->dropForeignKey('user_fk', 'wishlist');
  52 +
  53 + $this->dropTable('wishlist');
  54 + }
  55 + }
... ...
models/Customer.php
1 1 <?php
2 2  
3 3 namespace artbox\order\models;
4   -
  4 +
  5 + use artbox\catalog\models\Product;
  6 + use artbox\catalog\models\Variant;
5 7 use Yii;
6 8 use yii\base\NotSupportedException;
7 9 use yii\behaviors\TimestampBehavior;
  10 + use yii\db\ActiveQuery;
8 11 use yii\db\ActiveRecord;
9 12 use yii\web\IdentityInterface;
10   -
  13 +
11 14 /**
12 15 * This is the model class for table "customer".
13   - *
14   - * @property integer $id
  16 +
  17 +*
  18 +*@property integer $id
15 19 * @property string $username
16 20 * @property string $auth_key
17 21 * @property string $password_hash
... ... @@ -26,6 +30,7 @@
26 30 * @property integer $birthday
27 31 * @property string $city
28 32 * @property string $address
  33 + * @property array $wishlist
29 34 * @property Order[] $orders
30 35 */
31 36 class Customer extends ActiveRecord implements IdentityInterface
... ... @@ -35,6 +40,26 @@
35 40 const GENDER_MALE = 1;
36 41 const GENDER_FEMALE = 2;
37 42  
  43 + protected $wishlist = null;
  44 +
  45 + /**
  46 + * @return array|\yii\db\ActiveRecord[]
  47 + */
  48 + public function getWishlist()
  49 + {
  50 + if ($this->wishlist === null) {
  51 + $this->wishlist = Wishlist::find()
  52 + ->select('variant_id')
  53 + ->where(
  54 + [
  55 + 'user_id' => $this->id,
  56 + ]
  57 + )
  58 + ->column();
  59 + }
  60 + return $this->wishlist;
  61 + }
  62 +
38 63 public function getBirthDate()
39 64 {
40 65 if (empty($this->birthday)) {
... ... @@ -48,7 +73,7 @@
48 73 {
49 74 $this->birthday = strtotime($value);
50 75 }
51   -
  76 +
52 77 /**
53 78 * @inheritdoc
54 79 */
... ... @@ -56,7 +81,7 @@
56 81 {
57 82 return 'customer';
58 83 }
59   -
  84 +
60 85 /**
61 86 * @inheritdoc
62 87 */
... ... @@ -66,7 +91,7 @@
66 91 TimestampBehavior::className(),
67 92 ];
68 93 }
69   -
  94 +
70 95 /**
71 96 * @inheritdoc
72 97 */
... ... @@ -130,7 +155,7 @@
130 155 ],
131 156 ];
132 157 }
133   -
  158 +
134 159 /**
135 160 * @inheritdoc
136 161 */
... ... @@ -154,7 +179,7 @@
154 179 'address' => Yii::t('order', 'Address'),
155 180 ];
156 181 }
157   -
  182 +
158 183 /**
159 184 * @return \yii\db\ActiveQuery
160 185 */
... ... @@ -163,7 +188,35 @@
163 188 return $this->hasMany(Order::className(), [ 'user_id' => 'id' ])
164 189 ->inverseOf('customer');
165 190 }
166   -
  191 +
  192 + /**
  193 + * @return ActiveQuery
  194 + */
  195 + public function getWishProducts()
  196 + {
  197 + return $this->hasMany(Product::className(), [ 'id' => 'product_id' ])
  198 + ->viaTable(
  199 + 'wishlist',
  200 + [
  201 + 'user_id' => 'id',
  202 + ]
  203 + );
  204 + }
  205 +
  206 + /**
  207 + * @return ActiveQuery
  208 + */
  209 + public function getWishVariants()
  210 + {
  211 + return $this->hasMany(Variant::className(), [ 'id' => 'variant_id' ])
  212 + ->viaTable(
  213 + 'wishlist',
  214 + [
  215 + 'user_id' => 'id',
  216 + ]
  217 + );
  218 + }
  219 +
167 220 /**
168 221 * @inheritdoc
169 222 */
... ... @@ -176,7 +229,7 @@
176 229 ]
177 230 );
178 231 }
179   -
  232 +
180 233 /**
181 234 * @inheritdoc
182 235 */
... ... @@ -184,7 +237,7 @@
184 237 {
185 238 throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
186 239 }
187   -
  240 +
188 241 /**
189 242 * Finds user by username
190 243 *
... ... @@ -201,7 +254,7 @@
201 254 ]
202 255 );
203 256 }
204   -
  257 +
205 258 /**
206 259 * Finds user by password reset token
207 260 *
... ... @@ -214,7 +267,7 @@
214 267 if (!static::isPasswordResetTokenValid($token)) {
215 268 return null;
216 269 }
217   -
  270 +
218 271 return static::findOne(
219 272 [
220 273 'password_reset_token' => $token,
... ... @@ -222,7 +275,7 @@
222 275 ]
223 276 );
224 277 }
225   -
  278 +
226 279 /**
227 280 * Finds out if password reset token is valid
228 281 *
... ... @@ -235,12 +288,12 @@
235 288 if (empty($token)) {
236 289 return false;
237 290 }
238   -
  291 +
239 292 $timestamp = (int) substr($token, strrpos($token, '_') + 1);
240 293 $expire = Yii::$app->params[ 'user.passwordResetTokenExpire' ];
241 294 return $timestamp + $expire >= time();
242 295 }
243   -
  296 +
244 297 /**
245 298 * @inheritdoc
246 299 */
... ... @@ -248,7 +301,7 @@
248 301 {
249 302 return $this->getPrimaryKey();
250 303 }
251   -
  304 +
252 305 /**
253 306 * @inheritdoc
254 307 */
... ... @@ -256,7 +309,7 @@
256 309 {
257 310 return $this->auth_key;
258 311 }
259   -
  312 +
260 313 /**
261 314 * @inheritdoc
262 315 */
... ... @@ -264,7 +317,7 @@
264 317 {
265 318 return $this->getAuthKey() === $authKey;
266 319 }
267   -
  320 +
268 321 /**
269 322 * Validates password
270 323 *
... ... @@ -276,7 +329,7 @@
276 329 {
277 330 return Yii::$app->security->validatePassword($password, $this->password_hash);
278 331 }
279   -
  332 +
280 333 /**
281 334 * Generates password hash from password and sets it to the model
282 335 *
... ... @@ -286,7 +339,7 @@
286 339 {
287 340 $this->password_hash = Yii::$app->security->generatePasswordHash($password);
288 341 }
289   -
  342 +
290 343 /**
291 344 * Generates "remember me" authentication key
292 345 */
... ... @@ -294,7 +347,7 @@
294 347 {
295 348 $this->auth_key = Yii::$app->security->generateRandomString();
296 349 }
297   -
  350 +
298 351 /**
299 352 * Generates new password reset token
300 353 */
... ... @@ -302,7 +355,7 @@
302 355 {
303 356 $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
304 357 }
305   -
  358 +
306 359 /**
307 360 * Removes password reset token
308 361 */
... ...
models/Wishlist.php 0 → 100644
  1 +<?php
  2 +
  3 + namespace artbox\order\models;
  4 +
  5 + use yii\db\ActiveRecord;
  6 +
  7 + /**
  8 + * Class Wishlist
  9 + *
  10 + * @property integer $user_id
  11 + * @property integer $product_id
  12 + * @property integer $variant_id
  13 + * @package artbox\order\models
  14 + */
  15 + class Wishlist extends ActiveRecord
  16 + {
  17 + /**
  18 + * @inheritdoc
  19 + */
  20 + public static function tableName()
  21 + {
  22 + return 'wishlist';
  23 + }
  24 +
  25 + /**
  26 + * @inheritdoc
  27 + */
  28 + public static function primaryKey()
  29 + {
  30 + return [
  31 + 'variant_id',
  32 + 'user_id',
  33 + ];
  34 + }
  35 + }
0 36 \ No newline at end of file
... ...