123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace app\common\model;
- /**
- * 用户收藏模型
- * Class UserFavorite
- * @package app\common\model
- */
- class UserFavorite extends BaseModel
- {
- protected $name = 'user_favorites';
- /**
- * 获取收藏对应的多态模型。
- */
- public function target()
- {
- return $this->morphTo('target', [
- 1 => 'app\common\model\Goods', //商品
- ]);
- }
- //获取列表
- public function getList($userId, $target_type = 1, $exclude_target_id = [])
- {
- if (!empty($exclude_target_id)) {
- $this->where('target_id', 'not in', $exclude_target_id);
- }
- $list = $this->where('user_id', '=', $userId)
- ->where('target_type', '=', $target_type)->order(['id' => 'desc'])
- ->paginate();
- return $list;
- }
- public function getOne($user_id, $target_type, $target_id)
- {
- return $this->where('user_id', '=', $user_id)
- ->where('target_type', $target_type)
- ->where('target_id', $target_id)
- ->find();
- }
- public function addOne($user_id, $target_type, $target_id)
- {
- $data['user_id'] = $user_id;
- $data['target_type'] = $target_type;
- $data['target_id'] = $target_id;
- $this->save($data);
- }
- public function deleteOne($user_id, $target_type, $target_id)
- {
- $data['user_id'] = $user_id;
- $data['target_type'] = $target_type;
- $data['target_id'] = $target_id;
- $this->where($data)->delete();
- }
- public function getGoodCollect($user_id, $target_id, $target_type = 1)
- {
- if (empty($user_id)) {
- return false;
- }
- $one = $this->getOne($user_id, $target_type, $target_id);
- if (empty($one)) {
- return false;
- }
- return true;
- }
- }
|