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