ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ActiveRecord.php
Go to the documentation of this file.
1<?php
2require_once('class.ActiveRecordList.php');
3require_once('Connector/class.arConnector.php');
4require_once('Connector/class.arConnectorDB.php');
5require_once('Cache/class.arObjectCache.php');
6require_once('Fields/class.arFieldList.php');
7require_once('Cache/class.arFieldCache.php');
8require_once('Storage/int.arStorageInterface.php');
9require_once('Factory/class.arFactory.php');
10require_once('Cache/class.arCalledClassCache.php');
11require_once('Connector/class.arConnectorMap.php');
23abstract class ActiveRecord implements arStorageInterface {
24
25 const ACTIVE_RECORD_VERSION = '2.0.7';
29 //protected $arConnector;
33 //protected $arFieldList;
37 protected $ar_safe_read = true;
42
43
47 public function getArConnector() {
48 return arConnectorMap::get($this);
49 //return $this->arConnector;
50 }
51
52
56 public function getArFieldList() {
57 return arFieldCache::get($this);
58 // return $this->arFieldList;
59 }
60
61
67 abstract static function returnDbTableName();
68
69
74 public function getConnectorContainerName() {
75 // WILL BE ABSTRACT TO REPLACE returnDbTableName() IN NEXT VERSION
76 if ($this->connector_container_name) {
78 } else {
80
81 return $ar::returnDbTableName();
82 }
83 }
84
85
90 $this->connector_container_name = $connector_container_name;
91 }
92
93
97 public function getPrimaryFieldValue() {
98 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
99
100 return $this->{$primary_fieldname};
101 }
102
103
107 public function setPrimaryFieldValue($value) {
108 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
109
110 $this->{$primary_fieldname} = $value;
111 }
112
113
118 public function __construct($primary_key = 0, arConnector $connector = NULL) {
119 if($connector == NULL) {
120 $connector = new arConnectorDB();
121 }
122 //$this->arConnector = $connector;
123 arConnectorMap::register($this, $connector);
124
125 $arFieldList = arFieldCache::get($this);
126 //$this->arFieldList = $arFieldList ;
127 $key = $arFieldList->getPrimaryFieldName();
128 $this->{$key} = $primary_key;
129 if ($primary_key !== 0 AND $primary_key !== NULL AND $primary_key !== false) {
130 $this->read();
131 }
132 }
133
134
135 public function storeObjectToCache() {
137 }
138
139
145 public function __getConvertedDateFieldsAsArray($format = NULL) {
146 $converted_dates = array();
147 foreach ($this->getArFieldList()->getFields() as $field) {
148 if ($field->isDateField()) {
149 $name = $field->getName();
150 $value = $this->{$name};
151 $converted_dates[$name] = array(
152 'unformatted' => $value,
153 'unix' => strtotime($value),
154 );
155 if ($format) {
156 $converted_dates[$name]['formatted'] = date($format, strtotime($value));
157 }
158 }
159 }
160
161 return $converted_dates;
162 }
163
164
171 public function __asCsv($separator = ';', $header = false) {
172 $line = '';
173 if ($header) {
174 $line .= implode($separator, array_keys($this->getArFieldList()->getRawFields()));
175 $line .= "\n";
176 }
177 $array = array();
178 foreach ($this->__asArray() as $field_name => $value) {
179 $serialized = $this->serializeToCSV($field_name);
180 if ($serialized === NULL) {
181 $array[$field_name] = $this->{$field_name};
182 } else {
183 $array[$field_name] = $serialized;
184 }
185 }
186 $line .= implode($separator, array_values($array));
187
188 return $line;
189 }
190
191
200 protected function serializeToCSV($field) {
201 return NULL;
202 }
203
204
208 public function __asArray() {
209 $return = array();
210 foreach ($this->getArFieldList()->getFields() as $field) {
211 $fieldname = $field->getName();
212 $return[$fieldname] = $this->{$fieldname};
213 }
214
215 return $return;
216 }
217
218
222 public function __asStdClass() {
223 $return = new stdClass();
224 foreach ($this->getArFieldList()->getFields() as $field) {
225 $fieldname = $field->getName();
226 $return->{$fieldname} = $this->{$fieldname};
227 }
228
229 return $return;
230 }
231
232
236 public function __asSerializedObject() {
237 return serialize($this);
238 }
239
240
246 public function buildFromArray(array $array) {
247 $class = get_class($this);
248 $primary = $this->getArFieldList()->getPrimaryFieldName();
249 $primary_value = $array[$primary];
250 if ($primary_value AND arObjectCache::isCached($class, $primary_value)) {
251 return arObjectCache::get($class, $primary_value);
252 }
253 foreach ($array as $field_name => $value) {
254 if ($this->wakeUp($field_name, $value) === NULL) {
255 $this->{$field_name} = $value;
256 } else {
257 $this->{$field_name} = $this->wakeUp($field_name, $value);
258 }
259 }
261 $this->afterObjectLoad();
262
263 return $this;
264 }
265
266
272 public function sleep($field_name) {
273 return NULL;
274 }
275
276
283 public function wakeUp($field_name, $field_value) {
284 return NULL;
285 }
286
287
292 final public function getArrayForDb() {
293 return $this->getArrayForConnector();
294 }
295
296
300 final public function getArrayForConnector() {
301 $data = array();
302 foreach ($this->getArFieldList()->getFields() as $field) {
303 $field_name = $field->getName();
304 if ($this->sleep($field_name) === NULL) {
305 $data[$field_name] = array( $field->getFieldType(), $this->{$field_name} );
306 } else {
307 $data[$field_name] = array( $field->getFieldType(), $this->sleep($field_name) );
308 }
309 }
310
311 return $data;
312 }
313
314
315
316
317 //
318 // Collector Modifications
319 //
320
327 static protected function getCalledClass() {
328 $class = get_called_class();
329
330 return arCalledClassCache::get($class);
331 }
332
333
342 final public static function installDB() {
343 return self::getCalledClass()->installDatabase();
344 }
345
346
352 public function installConnector() {
353 return $this->installDatabase();
354 }
355
356
363 final public static function renameDBField($old_name, $new_name) {
364 return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
365 }
366
367
371 final public static function tableExists() {
372 return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
373 }
374
375
381 final public static function fieldExists($field_name) {
382 return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
383 }
384
385
391 final public static function removeDBField($field_name) {
392 return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
393 }
394
395
399 final protected function installDatabase() {
400 if (! $this->tableExists()) {
401 $fields = array();
402 foreach ($this->getArFieldList()->getFields() as $field) {
403 $fields[$field->getName()] = $field->getAttributesForConnector();
404 }
405
406 return $this->getArConnector()->installDatabase($this, $fields);
407 } else {
408 return $this->getArConnector()->updateDatabase($this);
409 }
410 }
411
412
416 final public static function updateDB() {
417 if (! self::tableExists()) {
418 self::getCalledClass()->installDatabase();
419
420 return true;
421 }
422
423 return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
424 }
425
426
430 final public static function resetDB() {
431 return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
432 }
433
434
438 final public static function truncateDB() {
439 return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
440 }
441
442
446 final public static function flushDB() {
447 return self::truncateDB();
448 }
449
450 //
451 // CRUD
452 //
453 public function store() {
454 if (! $this->getId()) {
455 $this->create();
456 } else {
457 $this->update();
458 }
459 }
460
461
462 public function save() {
463 $this->store();
464 }
465
466
467 public function create() {
468 if ($this->getArFieldList()->getPrimaryField()->getSequence()) {
469 $this->id = $this->getArConnector()->nextID($this);
470 }
471
472 $this->getArConnector()->create($this, $this->getArrayForConnector());
474 }
475
476
483 public function copy($new_id = 0) {
484 if (self::where(array( $this->getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
486 }
487 $new_obj = clone($this);
488 $new_obj->setPrimaryFieldValue($new_id);
489
490 return $new_obj;
491 }
492
493
494 public function afterObjectLoad() {
495 }
496
497
498 public function read() {
499 $records = $this->getArConnector()->read($this);
500 if (count($records) == 0 AND $this->ar_safe_read == true) {
501 throw new arException(arException::RECORD_NOT_FOUND, $this->getPrimaryFieldValue());
502 } elseif (count($records) == 0 AND $this->ar_safe_read == false) {
503 $this->is_new = true;
504 }
505 foreach ($records as $rec) {
506 foreach ($this->getArrayForConnector() as $k => $v) {
507 if ($this->wakeUp($k, $rec->{$k}) === NULL) {
508 $this->{$k} = $rec->{$k};
509 } else {
510 $this->{$k} = $this->wakeUp($k, $rec->{$k});
511 }
512 }
514 $this->afterObjectLoad();
515 }
516 }
517
518
519 public function update() {
520 $this->getArConnector()->update($this);
522 }
523
524
525 public function delete() {
526 $this->getArConnector()->delete($this);
528 }
529
530
531
532 //
533 // Collection
534 //
538 public static function preloadObjects() {
539 return self::get();
540 }
541
542
548 public static function additionalParams(array $additional_params) {
549 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
550 $srModelObjectList->additionalParams($additional_params);
551
552 return $srModelObjectList;
553 }
554
555
562 public static function find($primary_key, array $add_constructor_args = array()) {
566 try {
567 $class_name = get_called_class();
568 if (! arObjectCache::isCached($class_name, $primary_key)) {
569 $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
570 $obj->storeObjectToCache();
571
572 return $obj;
573 }
574 } catch (arException $e) {
575 return NULL;
576 }
577
578 try {
579 $obj = arObjectCache::get($class_name, $primary_key);
580 } catch (arException $e) {
581 return NULL;
582 }
583
584 return $obj;
585 }
586
587
596 public static function findOrFail($primary_key, array $add_constructor_args = array()) {
597 $obj = self::find($primary_key, $add_constructor_args);
598 if (is_null($obj)) {
600 }
601
602 return $obj;
603 }
604
605
614 public static function findOrGetInstance($primary_key, array $add_constructor_args = array()) {
615 $obj = self::find($primary_key, $add_constructor_args);
616 if ($obj !== NULL) {
617 return $obj;
618 } else {
619 $class_name = get_called_class();
620 $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
621 $obj->setPrimaryFieldValue($primary_key);
622 $obj->is_new = true;
623 $obj->storeObjectToCache();
624
625 return $obj;
626 }
627 }
628
629
636 public static function where($where, $operator = NULL) {
637 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
638 $srModelObjectList->where($where, $operator);
639
640 return $srModelObjectList;
641 }
642
643
653 public static function innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
654 return self::innerjoin($ar->getConnectorContainerName(), $on_this, $on_external, $fields, $operator, $both_external);
655 }
656
657
667 public static function innerjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
668 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
669
670 return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
671 }
672
673
683 public static function leftjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
684 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
685
686 return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
687 }
688
689
696 public static function orderBy($orderBy, $orderDirection = 'ASC') {
697 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
698 $srModelObjectList->orderBy($orderBy, $orderDirection);
699
700 return $srModelObjectList;
701 }
702
703
709 public static function dateFormat($date_format = 'd.m.Y - H:i:s') {
710 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
711 $srModelObjectList->dateFormat($date_format);
712
713 return $srModelObjectList;
714 }
715
716
723 public static function limit($start, $end) {
724 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
725 $srModelObjectList->limit($start, $end);
726
727 return $srModelObjectList;
728 }
729
730
734 public static function affectedRows() {
735 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
736
737 return $srModelObjectList->affectedRows();
738 }
739
740
744 public static function count() {
745 return self::affectedRows();
746 }
747
748
752 public static function get() {
753 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
754
755 return $srModelObjectList->get();
756 }
757
758
762 public static function debug() {
763 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
764
765 return $srModelObjectList->debug();
766 }
767
768
772 public static function first() {
773 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
774
775 return $srModelObjectList->first();
776 }
777
778
782 public static function getCollection() {
783 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
784
785 return $srModelObjectList;
786 }
787
788
792 public static function last() {
793 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
794
795 return $srModelObjectList->last();
796 }
797
798
803 public static function getFirstFromLastQuery() {
804 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
805
806 return $srModelObjectList->getFirstFromLastQuery();
807 }
808
809
815 public static function connector(arConnector $connector) {
816 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
817
818 return $srModelObjectList->connector($connector);
819 }
820
821
827 public static function raw($set_raw = true) {
828 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
829
830 return $srModelObjectList->raw($set_raw);
831 }
832
833
840 public static function getArray($key = NULL, $values = NULL) {
841 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
842
843 return $srModelObjectList->getArray($key, $values);
844 }
845
846 //
847 // Magic Methods & Helpers
848 //
855 public function __call($name, $arguments) {
856 // Getter
857 if (preg_match("/get([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 0) {
858 return $this->{self::fromCamelCase($matches[1])};
859 }
860 // Setter
861 if (preg_match("/set([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 1) {
862 $this->{self::fromCamelCase($matches[1])} = $arguments[0];
863 }
864 if (preg_match("/findBy([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 1) {
865 return self::where(array( self::fromCamelCase($matches[1]) => $arguments[0] ))->getFirst();
866 }
867 }
868
869
876 public static function _toCamelCase($str, $capitalise_first_char = false) {
877 if ($capitalise_first_char) {
878 $str[0] = strtoupper($str[0]);
879 }
880 $func = create_function('$c', 'return strtoupper($c[1]);');
881
882 return preg_replace_callback('/_([a-z])/', $func, $str);
883 }
884
885
891 protected static function fromCamelCase($str) {
892 $str[0] = strtolower($str[0]);
893 $func = create_function('$c', 'return "_" . strtolower($c[1]);');
894
895 return preg_replace_callback('/([A-Z])/', $func, $str);
896 }
897}
898
899?>
Class ActiveRecordList.
Class ActiveRecord.
static renameDBField($old_name, $new_name)
static _toCamelCase($str, $capitalise_first_char=false)
setPrimaryFieldValue($value)
serializeToCSV($field)
This method is called for every field of your instance if you use __asCsv.
static returnDbTableName()
static findOrFail($primary_key, array $add_constructor_args=array())
Tries to find the object and throws an Exception if object is not found, instead of returning null.
static connector(arConnector $connector)
sleep($field_name)
__call($name, $arguments)
static findOrGetInstance($primary_key, array $add_constructor_args=array())
static fieldExists($field_name)
buildFromArray(array $array)
static limit($start, $end)
static fromCamelCase($str)
setConnectorContainerName($connector_container_name)
static dateFormat($date_format='d.m.Y - H:i:s')
static removeDBField($field_name)
static leftjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
__construct($primary_key=0, arConnector $connector=NULL)
wakeUp($field_name, $field_value)
static innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static raw($set_raw=true)
static getFirstFromLastQuery()
static getArray($key=NULL, $values=NULL)
static additionalParams(array $additional_params)
static orderBy($orderBy, $orderDirection='ASC')
__getConvertedDateFieldsAsArray($format=NULL)
static innerjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static where($where, $operator=NULL)
__asCsv($separator=';', $header=false)
Class arConnectorDB.
static register(ActiveRecord $ar, arConnector $connector)
static get(ActiveRecord $ar)
Class arConnector.
Class arException.
const COPY_DESTINATION_ID_EXISTS
static getPrimaryFieldName(ActiveRecord $ar)
static get(ActiveRecord $ar)
static isCached($class, $id)
static store(ActiveRecord $object)
static purge(ActiveRecord $object)
static get($class, $id)
$header
$data
$separator
Class arStorageInterface.
$records
Definition: simple_test.php:17