ArticleSourceService.class.php
1.37 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/12
* Time: 10:46
*/
namespace Common\Service;
use Common\Model\ArticleSourceModel;
class ArticleSourceService extends AbstractService
{
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new ArticleSourceModel();
}
/**
* 保存课程素材关系
* @author zhonglei
* @param int $article_id 课程ID
* @param array $source_ids_post 素材ID数组
* @return bool
*/
public function saveData($article_id, $source_ids_post)
{
$article_sources = $this->list_by_conds(['article_id' => $article_id]);
$source_ids_db = array_column($article_sources, 'source_id');
$ids_del = array_diff($source_ids_db, $source_ids_post);
$ids_insert = array_diff($source_ids_post, $source_ids_db);
// 删除数据
if (!empty($ids_del)) {
$this->delete_by_conds(['article_id' => $article_id, 'source_id' => $ids_del]);
}
$insert_data = [];
// 新增数据
foreach ($ids_insert as $v) {
$insert_data[] = [
'article_id' => $article_id,
'source_id' => $v,
];
}
if (!empty($insert_data)) {
$this->insert_all($insert_data);
}
return true;
}
}