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