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');
68 if ($this->connector_container_name) {
73 return $ar::returnDbTableName();
92 return $this->{$primary_fieldname};
102 $this->{$primary_fieldname} = $value;
111 if ($connector ==
null) {
118 $key = $arFieldList->getPrimaryFieldName();
119 $this->{$key} = $primary_key;
120 if ($primary_key !== 0 AND $primary_key !==
null AND $primary_key !==
false) {
137 $converted_dates = array();
139 if ($field->isDateField()) {
140 $name = $field->getName();
141 $value = $this->{$name};
142 $converted_dates[$name] = array(
143 'unformatted' => $value,
144 'unix' => strtotime($value),
147 $converted_dates[$name][
'formatted'] =
date($format, strtotime($value));
152 return $converted_dates;
165 $line .= implode($separator, array_keys($this->
getArFieldList()->getRawFields()));
169 foreach ($this->
__asArray() as $field_name => $value) {
171 if ($serialized ===
null) {
172 $array[$field_name] = $this->{$field_name};
174 $array[$field_name] = $serialized;
177 $line .= implode($separator, array_values($array));
202 $fieldname = $field->getName();
203 $return[$fieldname] = $this->{$fieldname};
214 $return =
new stdClass();
216 $fieldname = $field->getName();
217 $return->{$fieldname} = $this->{$fieldname};
228 return serialize($this);
238 $class = get_class($this);
239 $primary = $this->getArFieldList()->getPrimaryFieldName();
240 $primary_value = $array[$primary];
244 foreach ($array as $field_name => $value) {
245 if ($this->wakeUp($field_name, $value) ===
null) {
246 $this->{$field_name} = $value;
248 $this->{$field_name} = $this->wakeUp($field_name, $value);
252 $this->afterObjectLoad();
264 if ($this->getArFieldList()->getFieldByName($field_name)->isDateField()) {
265 return $this->getArConnector()->fixDate($value);
277 public function sleep($field_name) {
288 public function wakeUp($field_name, $field_value) {
298 return $this->getArrayForConnector();
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 );
331 $class = get_called_class();
346 return self::getCalledClass()->installDatabase();
356 return $this->installDatabase();
367 return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
375 return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
385 return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
395 return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
403 if (!$this->tableExists()) {
405 foreach ($this->getArFieldList()->getFields() as $field) {
406 $fields[$field->getName()] = $field->getAttributesForConnector();
409 return $this->getArConnector()->installDatabase($this, $fields);
411 return $this->getArConnector()->updateDatabase($this);
420 if (!self::tableExists()) {
421 self::getCalledClass()->installDatabase();
426 return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
434 return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
442 return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
450 return self::truncateDB();
458 $primary_value = $this->getPrimaryFieldValue();
460 if (!self::where(array( $primary_fieldname => $primary_value ))->hasSets()) {
474 if ($this->getArFieldList()->getPrimaryField()->getSequence()) {
476 $this->{$primary_fieldname} = $this->getArConnector()->nextID($this);
479 $this->getArConnector()->create($this, $this->getArrayForConnector());
490 public function copy($new_id = 0) {
491 if (self::where(array( $this->getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
494 $new_obj = clone($this);
495 $new_obj->setPrimaryFieldValue($new_id);
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;
513 foreach ($this->getArrayForConnector() as $k => $v) {
514 if ($this->wakeUp($k, $rec->{$k}) ===
null) {
515 $this->{$k} = $rec->{$k};
517 $this->{$k} = $this->wakeUp($k, $rec->{$k});
521 $this->afterObjectLoad();
527 $this->getArConnector()->update($this);
532 public function delete() {
533 $this->getArConnector()->delete($this);
557 $srModelObjectList->additionalParams($additional_params);
559 return $srModelObjectList;
569 public static function find($primary_key, array $add_constructor_args = array()) {
574 $class_name = get_called_class();
576 $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
577 $obj->storeObjectToCache();
603 public static function findOrFail($primary_key, array $add_constructor_args = array()) {
604 $obj = self::find($primary_key, $add_constructor_args);
622 $obj = self::find($primary_key, $add_constructor_args);
626 $class_name = get_called_class();
627 $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
628 $obj->setPrimaryFieldValue($primary_key);
630 $obj->storeObjectToCache();
643 public static function where($where, $operator =
null) {
645 $srModelObjectList->where($where, $operator);
647 return $srModelObjectList;
660 public static function innerjoinAR(
ActiveRecord $ar, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
674 public static function innerjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
677 return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
690 public static function leftjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
693 return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
703 public static function orderBy($orderBy, $orderDirection =
'ASC') {
705 $srModelObjectList->orderBy($orderBy, $orderDirection);
707 return $srModelObjectList;
716 public static function dateFormat($date_format =
'd.m.Y - H:i:s') {
718 $srModelObjectList->dateFormat($date_format);
720 return $srModelObjectList;
732 $srModelObjectList->limit(
$start, $end);
734 return $srModelObjectList;
744 return $srModelObjectList->affectedRows();
752 return self::affectedRows();
759 public static function get() {
762 return $srModelObjectList->get();
772 return $srModelObjectList->debug();
782 return $srModelObjectList->first();
792 return $srModelObjectList;
799 public static function last() {
802 return $srModelObjectList->last();
813 return $srModelObjectList->getFirstFromLastQuery();
825 return $srModelObjectList->connector($connector);
834 public static function raw($set_raw =
true) {
837 return $srModelObjectList->raw($set_raw);
847 public static function getArray($key =
null, $values =
null) {
850 return $srModelObjectList->getArray($key, $values);
862 public function __call($name, $arguments) {
864 if (preg_match(
"/get([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 0) {
865 return $this->{self::fromCamelCase($matches[1])};
868 if (preg_match(
"/set([a-zA-Z]*)/u", $name, $matches) AND count($arguments) == 1) {
869 $this->{self::fromCamelCase($matches[1])} = $arguments[0];
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();
883 public static function _toCamelCase($str, $capitalise_first_char =
false) {
884 if ($capitalise_first_char) {
885 $str[0] = strtoupper($str[0]);
887 $func = create_function(
'$c',
'return strtoupper($c[1]);');
889 return preg_replace_callback(
'/_([a-z])/', $func, $str);
899 $str[0] = strtolower($str[0]);
900 $func = create_function(
'$c',
'return "_" . strtolower($c[1]);');
902 return preg_replace_callback(
'/([A-Z])/', $func, $str);
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static renameDBField($old_name, $new_name)
const ACTIVE_RECORD_VERSION
getConnectorContainerName()
static _toCamelCase($str, $capitalise_first_char=false)
__getConvertedDateFieldsAsArray($format=null)
setPrimaryFieldValue($value)
$connector_container_name
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)
__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 register(ActiveRecord $ar, arConnector $connector)
static get(ActiveRecord $ar)
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)
Class arStorageInterface.
get($resource)
Loads and returns the value for the supplied cache key.