1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Ivan Kerin <[email protected]> |
5
|
|
|
* @copyright (c) 2014 Clippings Ltd. |
6
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
class Kohana_Jam_Association_Taxonomy_Items extends Jam_Association_Collection { |
9
|
|
|
|
10
|
|
|
public $join_table_dependent = TRUE; |
11
|
|
|
|
12
|
|
|
public $item_key = 'item_id'; |
13
|
|
|
|
14
|
|
|
public $item_polymorphic_key = 'item_model'; |
15
|
|
|
|
16
|
|
|
public $term_key = 'term_id'; |
17
|
|
|
|
18
|
|
|
public $join_table = 'terms_items'; |
19
|
|
|
|
20
|
4 |
|
public function join($alias, $type = NULL) |
21
|
|
|
{ |
22
|
4 |
|
return Jam_Query_Builder_Join::factory($this->join_table, $type) |
23
|
4 |
|
->context_model($this->model) |
24
|
4 |
|
->model($this->foreign_model) |
25
|
4 |
|
->on($this->join_table.'.'.$this->item_key, '=', ':primary_key') |
26
|
4 |
|
->on(DB::expr(':model', array(':model' => $this->foreign_model)), '=' , $this->join_table.'.'.$this->item_polymorphic_key) |
27
|
4 |
|
->join_table($alias ? array($this->foreign_model, $alias) : $this->foreign_model, $type) |
28
|
4 |
|
->on(':primary_key', '=' , $this->join_table.'.'.$this->term_key) |
29
|
4 |
|
->context_model($this->model) |
30
|
4 |
|
->end(); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
public function collection(Jam_Model $model) |
34
|
|
|
{ |
35
|
4 |
|
$collection = Jam::all($this->foreign_model); |
36
|
|
|
|
37
|
|
|
return $collection |
38
|
4 |
|
->join_table($this->join_table) |
39
|
4 |
|
->context_model($this->foreign_model) |
40
|
4 |
|
->on($this->join_table.'.'.$this->item_polymorphic_key, '=', DB::expr(':model', array(':model' => $this->foreign_model))) |
41
|
4 |
|
->on($this->join_table.'.'.$this->item_key, '=', ':primary_key') |
42
|
4 |
|
->end() |
43
|
4 |
|
->where($this->join_table.'.'.$this->term_key, '=' , $model->id()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function model_after_delete(Jam_Model $model) |
47
|
|
|
{ |
48
|
|
|
if ($model->loaded() AND $this->join_table_dependent) |
49
|
|
|
{ |
50
|
|
|
$this->erase_query($model) |
51
|
|
|
->execute(Jam::meta($this->model)->db()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
public function erase_query(Jam_Model $model) |
56
|
|
|
{ |
57
|
2 |
|
return DB::delete($this->join_table) |
58
|
2 |
|
->where($this->term_key, '=', $model->id()); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function remove_items_query(Jam_Model $model, array $ids) |
62
|
|
|
{ |
63
|
2 |
|
return DB::delete($this->join_table) |
64
|
2 |
|
->where($this->term_key, '=', $model->id()) |
65
|
2 |
|
->where($this->item_polymorphic_key, '=', $this->foreign_model) |
66
|
2 |
|
->where($this->item_key, 'IN', $ids); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function add_items_query(Jam_Model $model, array $ids) |
70
|
|
|
{ |
71
|
2 |
|
$query = DB::insert($this->join_table) |
72
|
2 |
|
->columns(array($this->term_key, $this->item_polymorphic_key, $this->item_key)); |
73
|
|
|
|
74
|
2 |
|
foreach ($ids as $id) |
75
|
|
|
{ |
76
|
2 |
|
$query->values(array($model->id(), $this->foreign_model, $id)); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $query; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|