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');
72 if ($this->connector_container_name) {
77 return $ar::returnDbTableName();
98 return $this->{$primary_fieldname};
109 $this->{$primary_fieldname} = $value;
126 $key = $arFieldList->getPrimaryFieldName();
127 $this->{
$key} = $primary_key;
128 if ($primary_key !== 0 and $primary_key !==
null and $primary_key !==
false) {
147 $converted_dates = array();
149 if ($field->isDateField()) {
150 $name = $field->getName();
151 $value = $this->{
$name};
152 $converted_dates[
$name] = array(
153 'unformatted' => $value,
154 'unix' => strtotime($value),
157 $converted_dates[
$name][
'formatted'] = date(
$format, strtotime($value));
162 return $converted_dates;
176 $line .= implode($separator, array_keys($this->
getArFieldList()->getRawFields()));
180 foreach ($this->
__asArray() as $field_name => $value) {
182 if ($serialized ===
null) {
183 $array[$field_name] = $this->{$field_name};
185 $array[$field_name] = $serialized;
188 $line .= implode($separator, array_values($array));
215 $fieldname = $field->getName();
216 $return[$fieldname] = $this->{$fieldname};
228 $return =
new stdClass();
230 $fieldname = $field->getName();
231 $return->{$fieldname} = $this->{$fieldname};
243 return serialize($this);
254 $class = get_class($this);
255 $primary = $this->getArFieldList()->getPrimaryFieldName();
256 $primary_value = $array[$primary];
260 foreach ($array as $field_name => $value) {
261 $waked = $this->wakeUp($field_name, $value);
262 $this->{$field_name} = ($waked ===
null) ? $value : $waked;
265 $this->afterObjectLoad();
278 if ($this->getArFieldList()->getFieldByName($field_name)->isDateField()) {
279 return $this->getArConnector()->fixDate($value);
303 public function wakeUp($field_name, $field_value)
315 return $this->getArrayForConnector();
325 foreach ($this->getArFieldList()->getFields() as $field) {
326 $field_name = $field->getName();
327 $sleeped = $this->sleep($field_name);
328 $var = ($sleeped ===
null) ? ($this->{$field_name}) : $sleeped;
329 $data[$field_name] = array( $field->getFieldType(), $var );
350 $class = get_called_class();
366 return self::getCalledClass()->installDatabase();
377 return $this->installDatabase();
389 return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
398 return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
409 return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
420 return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
429 if (!$this->tableExists()) {
431 foreach ($this->getArFieldList()->getFields() as $field) {
432 $fields[$field->getName()] = $field->getAttributesForConnector();
435 return $this->getArConnector()->installDatabase($this, $fields);
437 return $this->getArConnector()->updateDatabase($this);
447 if (!self::tableExists()) {
448 self::getCalledClass()->installDatabase();
453 return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
462 return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
471 return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
480 return self::truncateDB();
489 $primary_value = $this->getPrimaryFieldValue();
491 if (!self::where(array( $primary_fieldname => $primary_value ))->hasSets()) {
507 if ($this->getArFieldList()->getPrimaryField()->getSequence()) {
509 $this->{$primary_fieldname} = $this->getArConnector()->nextID($this);
512 $this->getArConnector()->create($this, $this->getArrayForConnector());
523 public function copy($new_id = 0)
525 if (self::where(array( $this->getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
528 $new_obj = clone($this);
529 $new_obj->setPrimaryFieldValue($new_id);
545 $records = $this->getArConnector()->read($this);
546 if (is_array(
$records) && count(
$records) === 0 && $this->ar_safe_read ===
true) {
548 } elseif (is_array(
$records) && count(
$records) === 0 && $this->ar_safe_read ===
false) {
549 $this->is_new =
true;
553 foreach ($this->getArrayForConnector() as $k => $v) {
554 $waked = $this->wakeUp($k, $rec->{$k});
555 $this->{$k} = ($waked ===
null) ? $rec->{$k} : $waked;
558 $this->afterObjectLoad();
565 $this->getArConnector()->update($this);
570 public function delete()
572 $this->getArConnector()->delete($this);
598 $srModelObjectList->additionalParams($additional_params);
600 return $srModelObjectList;
610 public static function find($primary_key, array $add_constructor_args = array())
616 $class_name = get_called_class();
618 $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
619 $obj->storeObjectToCache();
645 public static function findOrFail($primary_key, array $add_constructor_args = array())
647 $obj = self::find($primary_key, $add_constructor_args);
666 $obj = self::find($primary_key, $add_constructor_args);
670 $class_name = get_called_class();
671 $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
672 $obj->setPrimaryFieldValue($primary_key);
674 $obj->storeObjectToCache();
687 public static function where($where, $operator =
null)
690 $srModelObjectList->where($where, $operator);
692 return $srModelObjectList;
705 public static function innerjoinAR(
ActiveRecord $ar, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false)
720 public static function innerjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false)
724 return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
737 public static function leftjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false)
741 return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
751 public static function orderBy($orderBy, $orderDirection =
'ASC')
754 $srModelObjectList->orderBy($orderBy, $orderDirection);
756 return $srModelObjectList;
765 public static function dateFormat($date_format =
'd.m.Y - H:i:s')
768 $srModelObjectList->dateFormat($date_format);
770 return $srModelObjectList;
785 return $srModelObjectList;
796 return $srModelObjectList->affectedRows();
805 return self::affectedRows();
812 public static function get()
816 return $srModelObjectList->get();
827 return $srModelObjectList->debug();
838 return $srModelObjectList->first();
849 return $srModelObjectList;
860 return $srModelObjectList->last();
872 return $srModelObjectList->getFirstFromLastQuery();
885 return $srModelObjectList->connector($connector);
894 public static function raw($set_raw =
true)
898 return $srModelObjectList->raw($set_raw);
912 return $srModelObjectList->getArray(
$key,
$values);
927 if (preg_match(
"/get([a-zA-Z]*)/u",
$name, $matches) and count($arguments) == 0) {
928 return $this->{self::fromCamelCase($matches[1])};
931 if (preg_match(
"/set([a-zA-Z]*)/u",
$name, $matches) and count($arguments) == 1) {
932 $this->{self::fromCamelCase($matches[1])} = $arguments[0];
934 if (preg_match(
"/findBy([a-zA-Z]*)/u",
$name, $matches) and count($arguments) == 1) {
935 return self::where(array( self::fromCamelCase($matches[1]) => $arguments[0] ))->getFirst();
946 public static function _toCamelCase($str, $capitalise_first_char =
false)
948 if ($capitalise_first_char) {
949 $str[0] = strtoupper($str[0]);
952 return preg_replace_callback(
'/_([a-z])/',
function (
$c) {
953 return strtoupper(
$c[1]);
965 $str[0] = strtolower($str[0]);
967 return preg_replace_callback(
'/([A-Z])/',
function (
$c) {
968 return "_" . strtolower(
$c[1]);
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 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.
update($pash, $contents, Config $config)