UserFavoriteModel.php
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
namespace api\user\model;
use api\common\model\CommonModel;
class UserFavoriteModel extends CommonModel
{
/**
* [base 基础查询条件]
*/
protected function base($query)
{
$query->field('id,title,url,description,create_time');
}
/**
* 关联表
* @param [string] $table_name [关联表名]
*/
protected function unionTable($table_name)
{
return $this->hasOne($table_name . 'Model', 'object_id');
}
/**
* url 自动转化
* @param $value
* @return string
*/
public function getUrlAttr($value)
{
$url = json_decode($value,true);
if (!empty($url)) {
$url = url($url['action'], $url['param'], true, true);
} else {
$url = '';
}
return $url;
}
/**
* 获取收藏内容
* @param [array] $data [select,find查询结果]
* @return [array] [收藏对应表的内容]
*/
public function getFavorite($data)
{
if (!is_string($data[0])) {
foreach ($data as $key => $value) {
$where[$value['table_name']][] = $value['object_id'];
}
foreach ($where as $key => $value) {
$favoriteData[] = $this->unionTable($key)->select($value);
}
} else {
$favoriteData = $this->unionTable($data['table_name'])->find($data['object_id']);
}
return $favoriteData;
}
/**
* [setFavorite 设置收藏]
* @Author: wuwu<15093565100@163.com>
* @DateTime: 2017-08-03T09:16:37+0800
* @since: 1.0
*/
public function setFavorite($data)
{
//获取收藏内容信息
$Favorite = self::create($data);
return $Favorite->id;
}
/**
* [unsetFavorite 取消收藏]
* @Author: wuwu<15093565100@163.com>
* @DateTime: 2017-08-03T09:17:30+0800
* @since: 1.0
* @return [type] [description]
*/
public function unsetFavorite($id)
{
return self::destroy($id); //执行删除
}
}