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');
72 if ($this->connector_container_name) {
75 $ar = self::getCalledClass();
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};
230 $fieldname = $field->getName();
231 $return->{$fieldname} = $this->{$fieldname};
243 return serialize($this);
254 $class = get_class($this);
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;
278 if ($this->
getArFieldList()->getFieldByName($field_name)->isDateField()) {
303 public function wakeUp($field_name, $field_value)
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();
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);
432 $fields[$field->getName()] = $field->getAttributesForConnector();
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();
491 if (!self::where(array( $primary_fieldname => $primary_value ))->hasSets()) {
509 $this->{$primary_fieldname} = $this->
getArConnector()->nextID($this);
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);
549 $this->is_new =
true;
554 $waked = $this->
wakeUp($k, $rec->{$k});
555 $this->{$k} = ($waked === null) ? $rec->{$k} : $waked;
570 public function delete()
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 removeDBField($field_name)
static limit($start, $end)
static getFirstFromLastQuery()
buildFromArray(array $array)
Class arStorageInterface.
static getPrimaryFieldName(ActiveRecord $ar)
static leftjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static renameDBField($old_name, $new_name)
static connector(arConnector $connector)
static innerjoinAR(ActiveRecord $ar, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
$connector_container_name
static innerjoin($tablename, $on_this, $on_external, $fields=array(' *'), $operator='=', $both_external=false)
static fieldExists($field_name)
static purge(ActiveRecord $object)
static returnDbTableName()
__call($name, $arguments)
static fromCamelCase($str)
__construct($primary_key=0, arConnector $connector=null)
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...
static store(ActiveRecord $object)
getConnectorContainerName()
wakeUp($field_name, $field_value)
static isCached($class, $id)
const ACTIVE_RECORD_VERSION
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)