-
Notifications
You must be signed in to change notification settings - Fork 0
/
Association.php
166 lines (133 loc) · 3.55 KB
/
Association.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace TRW\ActiveRecord;
use Exception;
use TRW\ActiveRecord\Util;
/**
* このクラスはAssociationのための基底クラス.
*
* HasOne, HasMany, BelongsTo, BelongsToManyはこのクラスを継承している
*
* @access private
*/
abstract class Association {
/**
* レコードクラスの名前空間.
*
* @var string
*/
private $recordNamespace;
/**
* 関連元のレコードクラス.
*
* @var string
*/
private $source;
/**
* 関連先のレコードクラス.
*
* @var string
*/
private $target;
/**
* 関連レコードを取得する際のデータの制御.
*
* @var array 次のキーが使える<br>
* $option = [<br>
* 'order' => 'id DESC', <br>
* 'limit' => 20, <br>
* 'offset' => 5 <br>
* ];
*/
private $option = [];
public function __construct($source, $target, $option = []){
$this->source = $source;
$this->target = $target;
$this->option = $option;
}
/**
* 関連元のレコードクラス名を返す.
*
* @return string 関連元のレコードクラス名
*/
public function source(){
return $this->source;
}
/**
* 関連先のレコードクラス名を返す.
*
* @return string 関連先のレコードクラス名
*/
public function target(){
return $this->target;
}
protected function mergeOption($conditions){
$conditions = array_merge($conditions, $this->option);
return $conditions;
}
public function getOption($key = null){
if($key === null){
return $this->option;
}
if(empty($this->option[$key])){
return false;
}
return $this->option[$key];
}
/**
* 関連先のレコードオブジェクトを取得する
*
* @param \TRW\ActiveRecord\BaseRecord $record 関連元のレコードオブジェクト
* @return \TRW\ActiveRecord\BaseRecord 関連先のレコードオブジェクト
*/
abstract public function find($record);
/**
* 関連元のレコードかか関連先のレコードか判断する
*
* @param TRW\ActiveRecord\BaseRecord $tableName レコードオブジェクト
* @return boolean @param $tableNameが関連元ならture そうでないならfalse
*/
abstract public function isOwningSide($tableName);
/**
* 関連先のレコードオブジェクトを保存する.
*
* @param \TRW\ActiveRecord\BaseRecord $record 関連元のレコードオブジェクト
* @return boolean 保存に成功すればtrue 失敗すればfalseを返す
*/
abstract public function save($record);
/**
* 与えられたクラス名に適切な名前空間名を付けて返す.
*
* @param string $name クラス名
* @return string 名前空間付きクラス名
*/
public function recordNamespace($name){
if($this->recordNamespace === null){
$source = $this->source;
$parts = explode('\\', $source);
array_pop($parts);
$namespace = implode('\\', $parts);
$this->recordNamespace = $namespace;
}
return $this->recordNamespace . '\\' . $name;
}
/**
* レコードクラスが使用しているテーブル名を返す.
*
* @param string レコードクラス名
* @return そのレコードクラスが使用しているテーブル名
*/
protected function createTableName($source){
list(, $class) = Util::namespaceSplit($source);
return Util::plural(lcfirst($class));
}
/**
* テーブルの外部キーを返す.
*
* @param string $name レコードクラス名
* @return そのレコードが使用しているテーブルの外部キー名
*/
protected function createForeignKey($name){
list(, $class) = Util::namespaceSplit($name);
return lcfirst($class) . '_id';
}
}