1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace app\common\model;
- /**
- * 用户分享模型
- * Class UserShare
- * @package app\common\model
- */
- class UserShare extends BaseModel
- {
- // target_type 类型 1-商品
- protected $name = 'user_share';
- /**
- * 获取分享对应的多态模型。
- */
- 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();
- }
- }
|