-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDBRelations.php
executable file
·86 lines (58 loc) · 1.97 KB
/
TDBRelations.php
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
<?php
/**
* relations model class
* @package Dimples
*
*/
class TDBRelations extends TDBEntities {
public $_table = ' relations';
public $_joins = array(
'entities' => array('table'=> 'entities','on'=>'entities.guid = relations.guid_b') ,
'revEntities' => array('table'=> 'entities','on'=>'entities.guid = relations.guid_a') ,
);
function addRelation($guid1 , $guid2 , $relationship)
{
$this->update('replace into relations set guid_a = '.$guid1.' , guid_b = '.$guid2.' ,
relationship = "'.$relationship.'"');
}
function removeRelation($guid1 , $guid2 , $relationship)
{
$this->update('delete from relations where guid_a = '.$guid1.' and guid_b = '.$guid2.' and
relationship = "'.$relationship.'"');
}
function removeAllRelations($guid1 , $guid2 )
{
$this->update('delete from relations where guid_a = '.$guid1.' and guid_b = '.$guid2.' ');
}
function getRelations($guid, $relationship,$entity_type = null)
{
$this->join('entities');
$sql = ' guid_a = '.$guid.' and relationship = "'.$relationship.'" ';
if (!empty($entity_type))
$sql .= ' and entities.entity_type = "'.$entity_type.'" ';
$this->find($sql);
}
function getRevRelations($guid, $relationship,$entity_type = null)
{
$this->join('revEntities');
$sql = ' guid_b = '.$guid.' and relationship = "'.$relationship.'" ';
if (!empty($entity_type))
$sql .= ' and entities.entity_type = "'.$entity_type.'" ';
$this->find($sql);
}
function next()
{
$data = parent::next();
if (empty($data))
return null;
$result = $this->getEntity($data);
return array_merge($result,$data);
}
function deleteEntity($guid)
{
db::startTransaction();
$this->update('delete from relations where guid = '.$this->escape($guid));
parent::deleteEntity($guid);
db::commit();
}
}