ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ActiveRecord.php
Go to the documentation of this file.
1 <?php
2 require_once('class.ActiveRecordList.php');
3 require_once('Connector/class.arConnector.php');
4 require_once('Connector/class.arConnectorDB.php');
5 require_once('Cache/class.arObjectCache.php');
6 require_once('Fields/class.arFieldList.php');
7 require_once('Cache/class.arFieldCache.php');
8 require_once('Storage/int.arStorageInterface.php');
9 require_once('Factory/class.arFactory.php');
10 require_once('Cache/class.arCalledClassCache.php');
11 require_once('Connector/class.arConnectorMap.php');
12 
24 abstract class ActiveRecord implements arStorageInterface {
25 
26  const ACTIVE_RECORD_VERSION = '2.0.7';
30  protected $ar_safe_read = true;
34  protected $connector_container_name = '';
35 
36 
40  public function getArConnector() {
41  return arConnectorMap::get($this);
42  }
43 
44 
48  public function getArFieldList() {
49  return arFieldCache::get($this);
50  }
51 
52 
57  public static function returnDbTableName() {
58  throw new arException(arException::UNKNONWN_EXCEPTION, 'Implement getConnectorContainerName in your child-class');
59  }
60 
61 
66  public function getConnectorContainerName() {
67  // WILL BE ABSTRACT TO REPLACE returnDbTableName() IN NEXT VERSION
68  if ($this->connector_container_name) {
70  } else {
71  $ar = self::getCalledClass();
72 
73  return $ar::returnDbTableName();
74  }
75  }
76 
77 
82  $this->connector_container_name = $connector_container_name;
83  }
84 
85 
89  public function getPrimaryFieldValue() {
90  $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
91 
92  return $this->{$primary_fieldname};
93  }
94 
95 
99  public function setPrimaryFieldValue($value) {
100  $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
101 
102  $this->{$primary_fieldname} = $value;
103  }
104 
105 
110  public function __construct($primary_key = 0, arConnector $connector = null) {
111  if ($connector == null) {
112  $connector = new arConnectorDB();
113  }
114  arConnectorMap::register($this, $connector);
115 
116  $arFieldList = arFieldCache::get($this);
117 
118  $key = $arFieldList->getPrimaryFieldName();
119  $this->{$key} = $primary_key;
120  if ($primary_key !== 0 AND $primary_key !== null AND $primary_key !== false) {
121  $this->read();
122  }
123  }
124 
125 
126  public function storeObjectToCache() {
127  arObjectCache::store($this);
128  }
129 
130 
136  public function __getConvertedDateFieldsAsArray($format = null) {
137  $converted_dates = array();
138  foreach ($this->getArFieldList()->getFields() as $field) {
139  if ($field->isDateField()) {
140  $name = $field->getName();
141  $value = $this->{$name};
142  $converted_dates[$name] = array(
143  'unformatted' => $value,
144  'unix' => strtotime($value),
145  );
146  if ($format) {
147  $converted_dates[$name]['formatted'] = date($format, strtotime($value));
148  }
149  }
150  }
151 
152  return $converted_dates;
153  }
154 
155 
162  public function __asCsv($separator = ';', $header = false) {
163  $line = '';
164  if ($header) {
165  $line .= implode($separator, array_keys($this->getArFieldList()->getRawFields()));
166  $line .= "\n";
167  }
168  $array = array();
169  foreach ($this->__asArray() as $field_name => $value) {
170  $serialized = $this->serializeToCSV($field_name);
171  if ($serialized === null) {
172  $array[$field_name] = $this->{$field_name};
173  } else {
174  $array[$field_name] = $serialized;
175  }
176  }
177  $line .= implode($separator, array_values($array));
178 
179  return $line;
180  }
181 
182 
191  protected function serializeToCSV($field) {
192  return null;
193  }
194 
195 
199  public function __asArray() {
200  $return = array();
201  foreach ($this->getArFieldList()->getFields() as $field) {
202  $fieldname = $field->getName();
203  $return[$fieldname] = $this->{$fieldname};
204  }
205 
206  return $return;
207  }
208 
209 
213  public function __asStdClass() {
214  $return = new stdClass();
215  foreach ($this->getArFieldList()->getFields() as $field) {
216  $fieldname = $field->getName();
217  $return->{$fieldname} = $this->{$fieldname};
218  }
219 
220  return $return;
221  }
222 
223 
227  public function __asSerializedObject() {
228  return serialize($this);
229  }
230 
231 
237  public function buildFromArray(array $array) {
238  $class = get_class($this);
239  $primary = $this->getArFieldList()->getPrimaryFieldName();
240  $primary_value = $array[$primary];
241  if ($primary_value AND arObjectCache::isCached($class, $primary_value)) {
242  return arObjectCache::get($class, $primary_value);
243  }
244  foreach ($array as $field_name => $value) {
245  if ($this->wakeUp($field_name, $value) === null) {
246  $this->{$field_name} = $value;
247  } else {
248  $this->{$field_name} = $this->wakeUp($field_name, $value);
249  }
250  }
251  arObjectCache::store($this);
252  $this->afterObjectLoad();
253 
254  return $this;
255  }
256 
257 
263  public function fixDateField($field_name, $value) {
264  if ($this->getArFieldList()->getFieldByName($field_name)->isDateField()) {
265  return $this->getArConnector()->fixDate($value);
266  }
267 
268  return $value;
269  }
270 
271 
277  public function sleep($field_name) {
278  return null;
279  }
280 
281 
288  public function wakeUp($field_name, $field_value) {
289  return null;
290  }
291 
292 
297  final public function getArrayForDb() {
298  return $this->getArrayForConnector();
299  }
300 
301 
305  final public function getArrayForConnector() {
306  $data = array();
307  foreach ($this->getArFieldList()->getFields() as $field) {
308  $field_name = $field->getName();
309  $sleeped = $this->sleep($field_name);
310  $var = ($sleeped === null) ? ($this->{$field_name}) : $sleeped;
311  $data[$field_name] = array( $field->getFieldType(), $var );
312  }
313 
314  return $data;
315  }
316 
317 
318 
319 
320  //
321  // Collector Modifications
322  //
323 
330  static protected function getCalledClass() {
331  $class = get_called_class();
332 
333  return arCalledClassCache::get($class);
334  }
335 
336 
345  final public static function installDB() {
346  return self::getCalledClass()->installDatabase();
347  }
348 
349 
355  public function installConnector() {
356  return $this->installDatabase();
357  }
358 
359 
366  final public static function renameDBField($old_name, $new_name) {
367  return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
368  }
369 
370 
374  final public static function tableExists() {
375  return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
376  }
377 
378 
384  final public static function fieldExists($field_name) {
385  return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
386  }
387 
388 
394  final public static function removeDBField($field_name) {
395  return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
396  }
397 
398 
402  final protected function installDatabase() {
403  if (!$this->tableExists()) {
404  $fields = array();
405  foreach ($this->getArFieldList()->getFields() as $field) {
406  $fields[$field->getName()] = $field->getAttributesForConnector();
407  }
408 
409  return $this->getArConnector()->installDatabase($this, $fields);
410  } else {
411  return $this->getArConnector()->updateDatabase($this);
412  }
413  }
414 
415 
419  final public static function updateDB() {
420  if (!self::tableExists()) {
421  self::getCalledClass()->installDatabase();
422 
423  return true;
424  }
425 
426  return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
427  }
428 
429 
433  final public static function resetDB() {
434  return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
435  }
436 
437 
441  final public static function truncateDB() {
442  return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
443  }
444 
445 
449  final public static function flushDB() {
450  return self::truncateDB();
451  }
452 
453  //
454  // CRUD
455  //
456  public function store() {
457  $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
458  $primary_value = $this->getPrimaryFieldValue();
459 
460  if (!self::where(array( $primary_fieldname => $primary_value ))->hasSets()) {
461  $this->create();
462  } else {
463  $this->update();
464  }
465  }
466 
467 
468  public function save() {
469  $this->store();
470  }
471 
472 
473  public function create() {
474  if ($this->getArFieldList()->getPrimaryField()->getSequence()) {
475  $primary_fieldname = arFieldCache::getPrimaryFieldName($this);
476  $this->{$primary_fieldname} = $this->getArConnector()->nextID($this);
477  }
478 
479  $this->getArConnector()->create($this, $this->getArrayForConnector());
480  arObjectCache::store($this);
481  }
482 
483 
490  public function copy($new_id = 0) {
491  if (self::where(array( $this->getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
493  }
494  $new_obj = clone($this);
495  $new_obj->setPrimaryFieldValue($new_id);
496 
497  return $new_obj;
498  }
499 
500 
501  public function afterObjectLoad() {
502  }
503 
504 
505  public function read() {
506  $records = $this->getArConnector()->read($this);
507  if (count($records) == 0 AND $this->ar_safe_read == true) {
509  } elseif (count($records) == 0 AND $this->ar_safe_read == false) {
510  $this->is_new = true;
511  }
512  foreach ($records as $rec) {
513  foreach ($this->getArrayForConnector() as $k => $v) {
514  if ($this->wakeUp($k, $rec->{$k}) === null) {
515  $this->{$k} = $rec->{$k};
516  } else {
517  $this->{$k} = $this->wakeUp($k, $rec->{$k});
518  }
519  }
520  arObjectCache::store($this);
521  $this->afterObjectLoad();
522  }
523  }
524 
525 
526  public function update() {
527  $this->getArConnector()->update($this);
528  arObjectCache::store($this);
529  }
530 
531 
532  public function delete() {
533  $this->getArConnector()->delete($this);
534  arObjectCache::purge($this);
535  }
536 
537 
538 
539  //
540  // Collection
541  //
545  public static function preloadObjects() {
546  return self::get();
547  }
548 
549 
555  public static function additionalParams(array $additional_params) {
556  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
557  $srModelObjectList->additionalParams($additional_params);
558 
559  return $srModelObjectList;
560  }
561 
562 
569  public static function find($primary_key, array $add_constructor_args = array()) {
573  try {
574  $class_name = get_called_class();
575  if (!arObjectCache::isCached($class_name, $primary_key)) {
576  $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
577  $obj->storeObjectToCache();
578 
579  return $obj;
580  }
581  } catch (arException $e) {
582  return null;
583  }
584 
585  try {
586  $obj = arObjectCache::get($class_name, $primary_key);
587  } catch (arException $e) {
588  return null;
589  }
590 
591  return $obj;
592  }
593 
594 
603  public static function findOrFail($primary_key, array $add_constructor_args = array()) {
604  $obj = self::find($primary_key, $add_constructor_args);
605  if (is_null($obj)) {
607  }
608 
609  return $obj;
610  }
611 
612 
621  public static function findOrGetInstance($primary_key, array $add_constructor_args = array()) {
622  $obj = self::find($primary_key, $add_constructor_args);
623  if ($obj !== null) {
624  return $obj;
625  } else {
626  $class_name = get_called_class();
627  $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
628  $obj->setPrimaryFieldValue($primary_key);
629  $obj->is_new = true;
630  $obj->storeObjectToCache();
631 
632  return $obj;
633  }
634  }
635 
636 
643  public static function where($where, $operator = null) {
644  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
645  $srModelObjectList->where($where, $operator);
646 
647  return $srModelObjectList;
648  }
649 
650 
660  public static function innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
661  return self::innerjoin($ar->getConnectorContainerName(), $on_this, $on_external, $fields, $operator, $both_external);
662  }
663 
664 
674  public static function innerjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
675  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
676 
677  return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
678  }
679 
680 
690  public static function leftjoin($tablename, $on_this, $on_external, $fields = array( '*' ), $operator = '=', $both_external = false) {
691  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
692 
693  return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
694  }
695 
696 
703  public static function orderBy($orderBy, $orderDirection = 'ASC') {
704  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
705  $srModelObjectList->orderBy($orderBy, $orderDirection);
706 
707  return $srModelObjectList;
708  }
709 
710 
716  public static function dateFormat($date_format = 'd.m.Y - H:i:s') {
717  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
718  $srModelObjectList->dateFormat($date_format);
719 
720  return $srModelObjectList;
721  }
722 
723 
730  public static function limit($start, $end) {
731  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
732  $srModelObjectList->limit($start, $end);
733 
734  return $srModelObjectList;
735  }
736 
737 
741  public static function affectedRows() {
742  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
743 
744  return $srModelObjectList->affectedRows();
745  }
746 
747 
751  public static function count() {
752  return self::affectedRows();
753  }
754 
755 
759  public static function get() {
760  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
761 
762  return $srModelObjectList->get();
763  }
764 
765 
769  public static function debug() {
770  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
771 
772  return $srModelObjectList->debug();
773  }
774 
775 
779  public static function first() {
780  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
781 
782  return $srModelObjectList->first();
783  }
784 
785 
789  public static function getCollection() {
790  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
791 
792  return $srModelObjectList;
793  }
794 
795 
799  public static function last() {
800  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
801 
802  return $srModelObjectList->last();
803  }
804 
805 
810  public static function getFirstFromLastQuery() {
811  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
812 
813  return $srModelObjectList->getFirstFromLastQuery();
814  }
815 
816 
822  public static function connector(arConnector $connector) {
823  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
824 
825  return $srModelObjectList->connector($connector);
826  }
827 
828 
834  public static function raw($set_raw = true) {
835  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
836 
837  return $srModelObjectList->raw($set_raw);
838  }
839 
840 
847  public static function getArray($key = null, $values = null) {
848  $srModelObjectList = new ActiveRecordList(self::getCalledClass());
849 
850  return $srModelObjectList->getArray($key, $values);
851  }
852 
853  //
854  // Magic Methods & Helpers
855  //
862  public function __call($name, $arguments) {
863  // Getter
864  if (preg_match("/get([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 0) {
865  return $this->{self::fromCamelCase($matches[1])};
866  }
867  // Setter
868  if (preg_match("/set([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 1) {
869  $this->{self::fromCamelCase($matches[1])} = $arguments[0];
870  }
871  if (preg_match("/findBy([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 1) {
872  return self::where(array( self::fromCamelCase($matches[1]) => $arguments[0] ))->getFirst();
873  }
874  }
875 
876 
883  public static function _toCamelCase($str, $capitalise_first_char = false) {
884  if ($capitalise_first_char) {
885  $str[0] = strtoupper($str[0]);
886  }
887  $func = create_function('$c', 'return strtoupper($c[1]);');
888 
889  return preg_replace_callback('/_([a-z])/', $func, $str);
890  }
891 
892 
898  protected static function fromCamelCase($str) {
899  $str[0] = strtolower($str[0]);
900  $func = create_function('$c', 'return "_" . strtolower($c[1]);');
901 
902  return preg_replace_callback('/([A-Z])/', $func, $str);
903  }
904 }
905 
906 ?>
static removeDBField($field_name)
static limit($start, $end)
static getFirstFromLastQuery()
buildFromArray(array $array)
Class arStorageInterface.
static getPrimaryFieldName(ActiveRecord $ar)
const UNKNONWN_EXCEPTION
static leftjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static renameDBField($old_name, $new_name)
Class arConnectorDB.
static connector(arConnector $connector)
static innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
Class ActiveRecord.
static innerjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static fieldExists($field_name)
static register(ActiveRecord $ar, arConnector $connector)
Class ActiveRecordList.
static purge(ActiveRecord $object)
sleep($field_name)
static returnDbTableName()
__call($name, $arguments)
Class arConnector.
static fromCamelCase($str)
__construct($primary_key=0, arConnector $connector=null)
$records
Definition: simple_test.php:22
static dateFormat($date_format='d.m.Y - H:i:s')
static where($where, $operator=null)
static raw($set_raw=true)
const COPY_DESTINATION_ID_EXISTS
setPrimaryFieldValue($value)
__getConvertedDateFieldsAsArray($format=null)
__asCsv($separator=';', $header=false)
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...
$header
static store(ActiveRecord $object)
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
Create styles array
The data for the language used.
static get($class, $id)
wakeUp($field_name, $field_value)
static isCached($class, $id)
Class arException.
fixDateField($field_name, $value)
static findOrGetInstance($primary_key, array $add_constructor_args=array())
static getArray($key=null, $values=null)
static get(ActiveRecord $ar)
setConnectorContainerName($connector_container_name)
serializeToCSV($field)
This method is called for every field of your instance if you use __asCsv.
static get(ActiveRecord $ar)
static orderBy($orderBy, $orderDirection='ASC')
static additionalParams(array $additional_params)
static _toCamelCase($str, $capitalise_first_char=false)