|
1
|
|
|
<?php |
|
2
|
|
|
namespace samson\activerecord; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class for defining permanent relations beetween database tables |
|
6
|
|
|
* @author Vitaly Iegorov <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class TableRelation |
|
10
|
|
|
{ |
|
11
|
|
|
/** Relation type: one-to-many */ |
|
12
|
|
|
const T_ONE_TO_MANY = 1; |
|
13
|
|
|
/** Relation type: one-to-one */ |
|
14
|
|
|
const T_ONE_TO_ONE = 0; |
|
15
|
|
|
|
|
16
|
|
|
/** Collection of permanent instances of class */ |
|
17
|
|
|
public static $instances = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** Parent table in relation */ |
|
20
|
|
|
public $parent; |
|
21
|
|
|
|
|
22
|
|
|
/** Child table in relation */ |
|
23
|
|
|
public $child; |
|
24
|
|
|
|
|
25
|
|
|
/** Parent table field name in relation */ |
|
26
|
|
|
public $parent_field; |
|
27
|
|
|
|
|
28
|
|
|
/** Child table field name in relation */ |
|
29
|
|
|
public $child_field; |
|
30
|
|
|
|
|
31
|
|
|
/** Alias for JOIN in relation */ |
|
32
|
|
|
public $alias; |
|
33
|
|
|
|
|
34
|
|
|
/** Relation type */ |
|
35
|
|
|
public $type; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $p Parent table name |
|
41
|
|
|
* @param string $c Child table name |
|
42
|
|
|
* @param string $pf Parent field name |
|
43
|
|
|
* @param integer $rt Relation type |
|
44
|
|
|
* @param string $cf Child field name |
|
45
|
|
|
* @param string $a Alias |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( $p, $c, $pf = null, $rt = self::T_ONE_TO_ONE, $cf = null, $a = null ) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
// Set table relation parameters |
|
50
|
|
|
$this->parent = $p; |
|
51
|
|
|
$this->child = $c; |
|
52
|
|
|
$this->alias = $a; |
|
53
|
|
|
$this->type = $rt; |
|
54
|
|
|
$this->parent_field = $pf; |
|
55
|
|
|
$this->child_field = $cf; |
|
56
|
|
|
|
|
57
|
|
|
/* |
|
58
|
|
|
// Get primary field |
|
59
|
|
|
eval('$primary = '.$p.'::$_primary;'); |
|
60
|
|
|
|
|
61
|
|
|
// If no primary field is specified - set parent primary field |
|
62
|
|
|
if( !isset($pf) ) $this->parent_field = $p.'.'.$primary; |
|
63
|
|
|
|
|
64
|
|
|
// If no child field is specified - set parent primary field |
|
65
|
|
|
if( !isset($cf) ) $this->child_field = $c.'.'.$primary; |
|
66
|
|
|
*/ |
|
67
|
|
|
|
|
68
|
|
|
// Save instance to static array |
|
69
|
|
|
self::$instances[] = & $this; |
|
70
|
|
|
} |
|
71
|
|
|
} |