KeywordRelated.php
1.3 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
<?php
namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
class KeywordRelated extends Base
{
//设置关联表名
protected $table = 'gl_product_keyword_related';
const CREATED_AT = null;
const UPDATED_AT = null;
//连接数据库
protected $connection = 'custom_mysql';
/**
* 关联产品关键词
* @param $product_id
* @param $keyword_ids
* @author zbj
* @date 2023/5/4
*/
public static function saveRelated($product_id, $keyword_ids,$type = 1)
{
if(!is_array($keyword_ids)){
$keyword_ids = array_filter(Arr::splitFilterToArray($keyword_ids), 'intval');
}
//先删除
self::where('product_id', $product_id)->where('type',$type)->delete();
//批量保存
$data = [];
if(!empty($keyword_ids)){
foreach ($keyword_ids as $keyword_id){
$data[] = [
'type' => $type,
'product_id' => $product_id,
'keyword_id' => $keyword_id,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s')
];
}
self::insert($data);
}
return true;
}
}