1: <?php
2:
3: namespace Alpha\Model\Type;
4:
5: use Alpha\Model\ActiveRecord;
6: use Alpha\Model\ActiveRecordProviderFactory;
7: use Alpha\Exception\FailedLookupCreateException;
8: use Alpha\Exception\IllegalArguementException;
9: use Alpha\Exception\AlphaException;
10: use Alpha\Util\Config\ConfigProvider;
11: use Alpha\Util\Logging\Logger;
12: use ReflectionClass;
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: class RelationLookup extends ActiveRecord implements TypeInterface
58: {
59: 60: 61: 62: 63: 64: 65:
66: protected $leftID;
67:
68: 69: 70: 71: 72: 73: 74:
75: protected $rightID;
76:
77: 78: 79: 80: 81: 82: 83:
84: private $leftClassName;
85:
86: 87: 88: 89: 90: 91: 92:
93: private $rightClassName;
94:
95: 96: 97: 98: 99: 100: 101:
102: private static $logger = null;
103:
104: 105: 106: 107: 108: 109: 110:
111: protected $dataLabels = array('OID' => 'RelationLookup ID#', 'leftID' => 'Left BO ID#', 'rightID' => 'Right BO ID#');
112:
113: 114: 115: 116: 117: 118: 119:
120: protected $helper = 'Not a valid RelationLookup value!';
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: public function __construct($leftClassName, $rightClassName)
131: {
132: self::$logger = new Logger('RelationLookup');
133: self::$logger->debug('>>__construct(leftClassName=['.$leftClassName.'], rightClassName=['.$rightClassName.'])');
134:
135:
136: parent::__construct();
137:
138: if (empty($leftClassName) || empty($rightClassName)) {
139: throw new IllegalArguementException('Cannot create RelationLookup object without providing the left and right class names!');
140: }
141:
142: $this->leftClassName = $leftClassName;
143: $this->rightClassName = $rightClassName;
144:
145: $this->leftID = new Integer();
146: $this->rightID = new Integer();
147:
148: $this->markTransient('leftClassName');
149: $this->markTransient('rightClassName');
150: $this->markTransient('helper');
151: $this->markTransient('TABLE_NAME');
152:
153:
154: $this->markUnique('leftID', 'rightID');
155:
156:
157: if (!$this->checkTableExists() && ActiveRecord::isInstalled()) {
158:
159: if (ActiveRecord::checkBOTableExists($leftClassName) && ActiveRecord::checkBOTableExists($rightClassName)) {
160: $this->makeTable();
161: } else {
162: throw new FailedLookupCreateException('Error trying to create a lookup table ['.$this->getTableName().'], as tables for BOs ['.$leftClassName.'] or ['.$rightClassName.'] don\'t exist!');
163: }
164: }
165:
166: self::$logger->debug('<<__construct');
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: public function getLeftClassName()
177: {
178: return $this->leftClassName;
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public function getRightClassName()
189: {
190: return $this->rightClassName;
191: }
192:
193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
203: public function getTableName()
204: {
205: if (isset($this->leftClassName) && isset($this->rightClassName)) {
206: $leftClass = new ReflectionClass($this->leftClassName);
207: $left = $leftClass->getShortname();
208: $rightClass = new ReflectionClass($this->rightClassName);
209: $right = $rightClass->getShortname();
210: self::$logger->debug('Setting table name to ['.$left.'2'.$right.']');
211:
212: return $left.'2'.$right;
213: } else {
214: throw new AlphaException('No table name set for the class ['.get_class($this).'], left or right class name(s) missing');
215: }
216: }
217:
218: 219: 220: 221: 222: 223: 224: 225:
226: public function loadAllByAttribute($attribute, $value, $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false, $constructorArgs = array())
227: {
228: if (!isset(self::$logger)) {
229: self::$logger = new Logger('RelationLookup');
230: }
231:
232: self::$logger->debug('>>loadAllByAttribute(attribute=['.$attribute.'], value=['.$value.'], start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.'], constructorArgs=['.print_r($constructorArgs, true).']');
233:
234: if (method_exists($this, 'before_loadAllByAttribute_callback')) {
235: $this->before_loadAllByAttribute_callback();
236: }
237:
238: $config = ConfigProvider::getInstance();
239:
240: $provider = ActiveRecordProviderFactory::getInstance($config->get('db.provider.name'), $this);
241: $objects = $provider->loadAllByAttribute($attribute, $value, $start, $limit, $orderBy, $order, $ignoreClassType, array($this->leftClassName, $this->rightClassName));
242:
243: if (method_exists($this, 'after_loadAllByAttribute_callback')) {
244: $this->after_loadAllByAttribute_callback();
245: }
246:
247: self::$logger->debug('<<loadAllByAttribute ['.count($objects).']');
248:
249: return $objects;
250: }
251:
252: 253: 254: 255: 256: 257: 258: 259:
260: public function loadAllByAttributes($attributes = array(), $values = array(), $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false)
261: {
262: self::$logger->debug('>>loadAllByAttributes(attributes=['.var_export($attributes, true).'], values=['.var_export($values, true).'], start=['.
263: $start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
264:
265: if (method_exists($this, 'before_loadAllByAttributes_callback')) {
266: $this->before_loadAllByAttributes_callback();
267: }
268:
269: $config = ConfigProvider::getInstance();
270:
271: if (!is_array($attributes) || !is_array($values)) {
272: throw new IllegalArguementException('Illegal arrays attributes=['.var_export($attributes, true).'] and values=['.var_export($values, true).
273: '] provided to loadAllByAttributes');
274: }
275:
276: $provider = ActiveRecordProviderFactory::getInstance($config->get('db.provider.name'), $this);
277: $objects = $provider->loadAllByAttributes($attributes, $values, $start, $limit, $orderBy, $order, $ignoreClassType, array($this->leftClassName, $this->rightClassName));
278:
279: if (method_exists($this, 'after_loadAllByAttributes_callback')) {
280: $this->after_loadAllByAttributes_callback();
281: }
282:
283: self::$logger->debug('<<loadAllByAttributes ['.count($objects).']');
284:
285: return $objects;
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: public function getHelper()
296: {
297: return $this->helper;
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public function setHelper($helper)
308: {
309: $this->helper = $helper;
310: }
311:
312: 313: 314: 315: 316: 317: 318:
319: public function getValue()
320: {
321: return array($this->leftID->getValue(), $this->rightID->getValue());
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function setValue($OIDs)
335: {
336: try {
337: $this->leftID->setValue($OIDs[0]);
338: $this->rightID->setValue($OIDs[1]);
339: } catch (\Exception $e) {
340: throw new IllegalArguementException('Array value passed to setValue is not valid ['.var_export($OIDs, true).'], array should contain two OIDs');
341: }
342: }
343:
344: 345: 346: 347: 348: 349: 350:
351: public function __toString()
352: {
353: return strval($this->getTableName());
354: }
355: }
356: