diff --git a/migrations/m170512_092259_create_wishlist_table.php b/migrations/m170512_092259_create_wishlist_table.php new file mode 100644 index 0000000..0dbfa33 --- /dev/null +++ b/migrations/m170512_092259_create_wishlist_table.php @@ -0,0 +1,55 @@ +createTable( + 'wishlist', + [ + 'user_id' => $this->integer(), + 'product_id' => $this->integer(), + 'variant_id' => $this->integer(), + ] + ); + + $this->addForeignKey('user_fk', 'wishlist', 'user_id', 'customer', 'id', 'CASCADE', 'CASCADE'); + $this->addForeignKey('product_fk', 'wishlist', 'product_id', 'product', 'id', 'CASCADE', 'CASCADE'); + $this->addForeignKey('variant_fk', 'wishlist', 'variant_id', 'variant', 'id', 'CASCADE', 'CASCADE'); + + $this->createIndex( + 'user_variant_fk', + 'wishlist', + [ + 'user_id', + 'variant_id', + ], + true + ); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropIndex( + 'user_variant_fk', + 'wishlist' + ); + + $this->dropForeignKey('variant_fk', 'wishlist'); + $this->dropForeignKey('product_fk', 'wishlist'); + $this->dropForeignKey('user_fk', 'wishlist'); + + $this->dropTable('wishlist'); + } + } diff --git a/models/Customer.php b/models/Customer.php index 34db3fe..77d8891 100644 --- a/models/Customer.php +++ b/models/Customer.php @@ -1,17 +1,21 @@ wishlist === null) { + $this->wishlist = Wishlist::find() + ->select('variant_id') + ->where( + [ + 'user_id' => $this->id, + ] + ) + ->column(); + } + return $this->wishlist; + } + public function getBirthDate() { if (empty($this->birthday)) { @@ -48,7 +73,7 @@ { $this->birthday = strtotime($value); } - + /** * @inheritdoc */ @@ -56,7 +81,7 @@ { return 'customer'; } - + /** * @inheritdoc */ @@ -66,7 +91,7 @@ TimestampBehavior::className(), ]; } - + /** * @inheritdoc */ @@ -130,7 +155,7 @@ ], ]; } - + /** * @inheritdoc */ @@ -154,7 +179,7 @@ 'address' => Yii::t('order', 'Address'), ]; } - + /** * @return \yii\db\ActiveQuery */ @@ -163,7 +188,35 @@ return $this->hasMany(Order::className(), [ 'user_id' => 'id' ]) ->inverseOf('customer'); } - + + /** + * @return ActiveQuery + */ + public function getWishProducts() + { + return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) + ->viaTable( + 'wishlist', + [ + 'user_id' => 'id', + ] + ); + } + + /** + * @return ActiveQuery + */ + public function getWishVariants() + { + return $this->hasMany(Variant::className(), [ 'id' => 'variant_id' ]) + ->viaTable( + 'wishlist', + [ + 'user_id' => 'id', + ] + ); + } + /** * @inheritdoc */ @@ -176,7 +229,7 @@ ] ); } - + /** * @inheritdoc */ @@ -184,7 +237,7 @@ { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } - + /** * Finds user by username * @@ -201,7 +254,7 @@ ] ); } - + /** * Finds user by password reset token * @@ -214,7 +267,7 @@ if (!static::isPasswordResetTokenValid($token)) { return null; } - + return static::findOne( [ 'password_reset_token' => $token, @@ -222,7 +275,7 @@ ] ); } - + /** * Finds out if password reset token is valid * @@ -235,12 +288,12 @@ if (empty($token)) { return false; } - + $timestamp = (int) substr($token, strrpos($token, '_') + 1); $expire = Yii::$app->params[ 'user.passwordResetTokenExpire' ]; return $timestamp + $expire >= time(); } - + /** * @inheritdoc */ @@ -248,7 +301,7 @@ { return $this->getPrimaryKey(); } - + /** * @inheritdoc */ @@ -256,7 +309,7 @@ { return $this->auth_key; } - + /** * @inheritdoc */ @@ -264,7 +317,7 @@ { return $this->getAuthKey() === $authKey; } - + /** * Validates password * @@ -276,7 +329,7 @@ { return Yii::$app->security->validatePassword($password, $this->password_hash); } - + /** * Generates password hash from password and sets it to the model * @@ -286,7 +339,7 @@ { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } - + /** * Generates "remember me" authentication key */ @@ -294,7 +347,7 @@ { $this->auth_key = Yii::$app->security->generateRandomString(); } - + /** * Generates new password reset token */ @@ -302,7 +355,7 @@ { $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); } - + /** * Removes password reset token */ diff --git a/models/Wishlist.php b/models/Wishlist.php new file mode 100644 index 0000000..5e1085a --- /dev/null +++ b/models/Wishlist.php @@ -0,0 +1,35 @@ +