ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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');
12
24abstract class ActiveRecord implements arStorageInterface
25{
26 const ACTIVE_RECORD_VERSION = '2.0.7';
30 protected $ar_safe_read = true;
35
36
40 public function getArConnector()
41 {
42 return arConnectorMap::get($this);
43 }
44
45
49 public function getArFieldList()
50 {
51 return arFieldCache::get($this);
52 }
53
54
59 public static function returnDbTableName()
60 {
61 throw new arException(arException::UNKNONWN_EXCEPTION, 'Implement getConnectorContainerName in your child-class');
62 }
63
64
69 public function getConnectorContainerName()
70 {
71 // WILL BE ABSTRACT TO REPLACE returnDbTableName() IN NEXT VERSION
72 if ($this->connector_container_name) {
74 } else {
76
77 return $ar::returnDbTableName();
78 }
79 }
80
81
86 {
87 $this->connector_container_name = $connector_container_name;
88 }
89
90
94 public function getPrimaryFieldValue()
95 {
96 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
97
98 return $this->{$primary_fieldname};
99 }
100
101
105 public function setPrimaryFieldValue($value)
106 {
107 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
108
109 $this->{$primary_fieldname} = $value;
110 }
111
112
117 public function __construct($primary_key = 0, arConnector $connector = null)
118 {
119 // if ($connector == null) {
120 // $connector = new arConnectorDB();
121 // }
122 // arConnectorMap::register($this, $connector);
123
124 $arFieldList = arFieldCache::get($this);
125
126 $key = $arFieldList->getPrimaryFieldName();
127 $this->{$key} = $primary_key;
128 if ($primary_key !== 0 and $primary_key !== null and $primary_key !== false) {
129 $this->read();
130 }
131 }
132
133
134 public function storeObjectToCache()
135 {
137 }
138
139
146 {
147 $converted_dates = array();
148 foreach ($this->getArFieldList()->getFields() as $field) {
149 if ($field->isDateField()) {
150 $name = $field->getName();
151 $value = $this->{$name};
152 $converted_dates[$name] = array(
153 'unformatted' => $value,
154 'unix' => strtotime($value),
155 );
156 if ($format) {
157 $converted_dates[$name]['formatted'] = date($format, strtotime($value));
158 }
159 }
160 }
161
162 return $converted_dates;
163 }
164
165
172 public function __asCsv($separator = ';', $header = false)
173 {
174 $line = '';
175 if ($header) {
176 $line .= implode($separator, array_keys($this->getArFieldList()->getRawFields()));
177 $line .= "\n";
178 }
179 $array = array();
180 foreach ($this->__asArray() as $field_name => $value) {
181 $serialized = $this->serializeToCSV($field_name);
182 if ($serialized === null) {
183 $array[$field_name] = $this->{$field_name};
184 } else {
185 $array[$field_name] = $serialized;
186 }
187 }
188 $line .= implode($separator, array_values($array));
189
190 return $line;
191 }
192
193
202 protected function serializeToCSV($field)
203 {
204 return null;
205 }
206
207
211 public function __asArray()
212 {
213 $return = array();
214 foreach ($this->getArFieldList()->getFields() as $field) {
215 $fieldname = $field->getName();
216 $return[$fieldname] = $this->{$fieldname};
217 }
218
219 return $return;
220 }
221
222
226 public function __asStdClass()
227 {
228 $return = new stdClass();
229 foreach ($this->getArFieldList()->getFields() as $field) {
230 $fieldname = $field->getName();
231 $return->{$fieldname} = $this->{$fieldname};
232 }
233
234 return $return;
235 }
236
237
241 public function __asSerializedObject()
242 {
243 return serialize($this);
244 }
245
246
252 public function buildFromArray(array $array)
253 {
254 $class = get_class($this);
255 $primary = $this->getArFieldList()->getPrimaryFieldName();
256 $primary_value = $array[$primary];
257 if ($primary_value and arObjectCache::isCached($class, $primary_value)) {
258 return arObjectCache::get($class, $primary_value);
259 }
260 foreach ($array as $field_name => $value) {
261 if ($this->wakeUp($field_name, $value) === null) {
262 $this->{$field_name} = $value;
263 } else {
264 $this->{$field_name} = $this->wakeUp($field_name, $value);
265 }
266 }
268 $this->afterObjectLoad();
269
270 return $this;
271 }
272
273
279 public function fixDateField($field_name, $value)
280 {
281 if ($this->getArFieldList()->getFieldByName($field_name)->isDateField()) {
282 return $this->getArConnector()->fixDate($value);
283 }
284
285 return $value;
286 }
287
288
294 public function sleep($field_name)
295 {
296 return null;
297 }
298
299
306 public function wakeUp($field_name, $field_value)
307 {
308 return null;
309 }
310
311
316 final public function getArrayForDb()
317 {
318 return $this->getArrayForConnector();
319 }
320
321
325 final public function getArrayForConnector()
326 {
327 $data = array();
328 foreach ($this->getArFieldList()->getFields() as $field) {
329 $field_name = $field->getName();
330 $sleeped = $this->sleep($field_name);
331 $var = ($sleeped === null) ? ($this->{$field_name}) : $sleeped;
332 $data[$field_name] = array( $field->getFieldType(), $var );
333 }
334
335 return $data;
336 }
337
338
339
340
341 //
342 // Collector Modifications
343 //
344
351 protected static function getCalledClass()
352 {
353 $class = get_called_class();
354
355 return arCalledClassCache::get($class);
356 }
357
358
367 final public static function installDB()
368 {
369 return self::getCalledClass()->installDatabase();
370 }
371
372
378 public function installConnector()
379 {
380 return $this->installDatabase();
381 }
382
383
390 final public static function renameDBField($old_name, $new_name)
391 {
392 return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
393 }
394
395
399 final public static function tableExists()
400 {
401 return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
402 }
403
404
410 final public static function fieldExists($field_name)
411 {
412 return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
413 }
414
415
421 final public static function removeDBField($field_name)
422 {
423 return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
424 }
425
426
430 final protected function installDatabase()
431 {
432 if (!$this->tableExists()) {
433 $fields = array();
434 foreach ($this->getArFieldList()->getFields() as $field) {
435 $fields[$field->getName()] = $field->getAttributesForConnector();
436 }
437
438 return $this->getArConnector()->installDatabase($this, $fields);
439 } else {
440 return $this->getArConnector()->updateDatabase($this);
441 }
442 }
443
444
448 final public static function updateDB()
449 {
450 if (!self::tableExists()) {
451 self::getCalledClass()->installDatabase();
452
453 return true;
454 }
455
456 return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
457 }
458
459
463 final public static function resetDB()
464 {
465 return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
466 }
467
468
472 final public static function truncateDB()
473 {
474 return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
475 }
476
477
481 final public static function flushDB()
482 {
483 return self::truncateDB();
484 }
485
486 //
487 // CRUD
488 //
489 public function store()
490 {
491 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
492 $primary_value = $this->getPrimaryFieldValue();
493
494 if (!self::where(array( $primary_fieldname => $primary_value ))->hasSets()) {
495 $this->create();
496 } else {
497 $this->update();
498 }
499 }
500
501
502 public function save()
503 {
504 $this->store();
505 }
506
507
508 public function create()
509 {
510 if ($this->getArFieldList()->getPrimaryField()->getSequence()) {
511 $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
512 $this->{$primary_fieldname} = $this->getArConnector()->nextID($this);
513 }
514
515 $this->getArConnector()->create($this, $this->getArrayForConnector());
517 }
518
519
526 public function copy($new_id = 0)
527 {
528 if (self::where(array( $this->getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
530 }
531 $new_obj = clone($this);
532 $new_obj->setPrimaryFieldValue($new_id);
533
534 return $new_obj;
535 }
536
537
538 public function afterObjectLoad()
539 {
540 }
541
542
546 public function read()
547 {
548 $records = $this->getArConnector()->read($this);
549 if (is_array($records) && count($records) === 0 && $this->ar_safe_read === true) {
550 throw new arException(arException::RECORD_NOT_FOUND, $this->getPrimaryFieldValue());
551 } elseif (is_array($records) && count($records) === 0 && $this->ar_safe_read === false) {
552 $this->is_new = true;
553 }
554 $records = is_array($records) ? $records : array();
555 foreach ($records as $rec) {
556 foreach ($this->getArrayForConnector() as $k => $v) {
557 if ($this->wakeUp($k, $rec->{$k}) === null) {
558 $this->{$k} = $rec->{$k};
559 } else {
560 $this->{$k} = $this->wakeUp($k, $rec->{$k});
561 }
562 }
564 $this->afterObjectLoad();
565 }
566 }
567
568
569 public function update()
570 {
571 $this->getArConnector()->update($this);
573 }
574
575
576 public function delete()
577 {
578 $this->getArConnector()->delete($this);
580 }
581
582
583
584 //
585 // Collection
586 //
590 public static function preloadObjects()
591 {
592 return self::get();
593 }
594
595
601 public static function additionalParams(array $additional_params)
602 {
603 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
604 $srModelObjectList->additionalParams($additional_params);
605
606 return $srModelObjectList;
607 }
608
609
616 public static function find($primary_key, array $add_constructor_args = array())
617 {
621 try {
622 $class_name = get_called_class();
623 if (!arObjectCache::isCached($class_name, $primary_key)) {
624 $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
625 $obj->storeObjectToCache();
626
627 return $obj;
628 }
629 } catch (arException $e) {
630 return null;
631 }
632
633 try {
634 $obj = arObjectCache::get($class_name, $primary_key);
635 } catch (arException $e) {
636 return null;
637 }
638
639 return $obj;
640 }
641
642
651 public static function findOrFail($primary_key, array $add_constructor_args = array())
652 {
653 $obj = self::find($primary_key, $add_constructor_args);
654 if (is_null($obj)) {
656 }
657
658 return $obj;
659 }
660
661
670 public static function findOrGetInstance($primary_key, array $add_constructor_args = array())
671 {
672 $obj = self::find($primary_key, $add_constructor_args);
673 if ($obj !== null) {
674 return $obj;
675 } else {
676 $class_name = get_called_class();
677 $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
678 $obj->setPrimaryFieldValue($primary_key);
679 $obj->is_new = true;
680 $obj->storeObjectToCache();
681
682 return $obj;
683 }
684 }
685
686
693 public static function where($where, $operator = null)
694 {
695 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
696 $srModelObjectList->where($where, $operator);
697
698 return $srModelObjectList;
699 }
700
701
711 public static function innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false)
712 {
713 return self::innerjoin($ar->getConnectorContainerName(), $on_this, $on_external, $fields, $operator, $both_external);
714 }
715
716
726 public static function innerjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false)
727 {
728 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
729
730 return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
731 }
732
733
743 public static function leftjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false)
744 {
745 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
746
747 return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
748 }
749
750
757 public static function orderBy($orderBy, $orderDirection = 'ASC')
758 {
759 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
760 $srModelObjectList->orderBy($orderBy, $orderDirection);
761
762 return $srModelObjectList;
763 }
764
765
771 public static function dateFormat($date_format = 'd.m.Y - H:i:s')
772 {
773 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
774 $srModelObjectList->dateFormat($date_format);
775
776 return $srModelObjectList;
777 }
778
779
786 public static function limit($start, $end)
787 {
788 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
789 $srModelObjectList->limit($start, $end);
790
791 return $srModelObjectList;
792 }
793
794
798 public static function affectedRows()
799 {
800 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
801
802 return $srModelObjectList->affectedRows();
803 }
804
805
809 public static function count()
810 {
811 return self::affectedRows();
812 }
813
814
818 public static function get()
819 {
820 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
821
822 return $srModelObjectList->get();
823 }
824
825
829 public static function debug()
830 {
831 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
832
833 return $srModelObjectList->debug();
834 }
835
836
840 public static function first()
841 {
842 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
843
844 return $srModelObjectList->first();
845 }
846
847
851 public static function getCollection()
852 {
853 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
854
855 return $srModelObjectList;
856 }
857
858
862 public static function last()
863 {
864 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
865
866 return $srModelObjectList->last();
867 }
868
869
874 public static function getFirstFromLastQuery()
875 {
876 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
877
878 return $srModelObjectList->getFirstFromLastQuery();
879 }
880
881
887 public static function connector(arConnector $connector)
888 {
889 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
890
891 return $srModelObjectList->connector($connector);
892 }
893
894
900 public static function raw($set_raw = true)
901 {
902 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
903
904 return $srModelObjectList->raw($set_raw);
905 }
906
907
914 public static function getArray($key = null, $values = null)
915 {
916 $srModelObjectList = new ActiveRecordList(self::getCalledClass());
917
918 return $srModelObjectList->getArray($key, $values);
919 }
920
921 //
922 // Magic Methods & Helpers
923 //
930 public function __call($name, $arguments)
931 {
932 // Getter
933 if (preg_match("/get([a-zA-Z]*)/u", $name, $matches) and count($arguments) == 0) {
934 return $this->{self::fromCamelCase($matches[1])};
935 }
936 // Setter
937 if (preg_match("/set([a-zA-Z]*)/u", $name, $matches) and count($arguments) == 1) {
938 $this->{self::fromCamelCase($matches[1])} = $arguments[0];
939 }
940 if (preg_match("/findBy([a-zA-Z]*)/u", $name, $matches) and count($arguments) == 1) {
941 return self::where(array( self::fromCamelCase($matches[1]) => $arguments[0] ))->getFirst();
942 }
943 }
944
945
952 public static function _toCamelCase($str, $capitalise_first_char = false)
953 {
954 if ($capitalise_first_char) {
955 $str[0] = strtoupper($str[0]);
956 }
957 $func = create_function('$c', 'return strtoupper($c[1]);');
958
959 return preg_replace_callback('/_([a-z])/', $func, $str);
960 }
961
962
968 protected static function fromCamelCase($str)
969 {
970 $str[0] = strtolower($str[0]);
971 $func = create_function('$c', 'return "_" . strtolower($c[1]);');
972
973 return preg_replace_callback('/([A-Z])/', $func, $str);
974 }
975}
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
Class ActiveRecordList.
Class ActiveRecord.
static renameDBField($old_name, $new_name)
static _toCamelCase($str, $capitalise_first_char=false)
__getConvertedDateFieldsAsArray($format=null)
setPrimaryFieldValue($value)
serializeToCSV($field)
This method is called for every field of your instance if you use __asCsv.
static where($where, $operator=null)
static returnDbTableName()
fixDateField($field_name, $value)
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 getArray($key=null, $values=null)
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)
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 additionalParams(array $additional_params)
static orderBy($orderBy, $orderDirection='ASC')
static innerjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
__construct($primary_key=0, arConnector $connector=null)
__asCsv($separator=';', $header=false)
An exception for terminatinating execution or to throw for unit testing.
static get(ActiveRecord $ar)
Class arConnector.
Class arException.
const COPY_DESTINATION_ID_EXISTS
const UNKNONWN_EXCEPTION
static getPrimaryFieldName(ActiveRecord $ar)
static get(ActiveRecord $ar)
static isCached($class, $id)
static store(ActiveRecord $object)
static purge(ActiveRecord $object)
static get($class, $id)
$key
Definition: croninfo.php:18
Class arStorageInterface.
if($format !==null) $name
Definition: metadata.php:146
$format
Definition: metadata.php:141
$end
Definition: saml1-acs.php:18
update($pash, $contents, Config $config)
$records
Definition: simple_test.php:22