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');
76 if ($this->connector_container_name) {
79 $ar = self::getCalledClass();
81 return $ar::returnDbTableName();
100 return $this->{$primary_fieldname};
110 $this->{$primary_fieldname} = $value;
119 if($connector == NULL) {
127 $key = $arFieldList->getPrimaryFieldName();
128 $this->{$key} = $primary_key;
129 if ($primary_key !== 0 AND $primary_key !== NULL AND $primary_key !==
false) {
146 $converted_dates = array();
148 if ($field->isDateField()) {
149 $name = $field->getName();
150 $value = $this->{$name};
151 $converted_dates[$name] = array(
152 'unformatted' => $value,
153 'unix' => strtotime($value),
156 $converted_dates[$name][
'formatted'] = date($format, strtotime($value));
161 return $converted_dates;
178 foreach ($this->
__asArray() as $field_name => $value) {
180 if ($serialized === NULL) {
181 $array[$field_name] = $this->{$field_name};
183 $array[$field_name] = $serialized;
186 $line .= implode(
$separator, array_values($array));
211 $fieldname = $field->getName();
212 $return[$fieldname] = $this->{$fieldname};
223 $return =
new stdClass();
225 $fieldname = $field->getName();
226 $return->{$fieldname} = $this->{$fieldname};
237 return serialize($this);
247 $class = get_class($this);
249 $primary_value = $array[$primary];
253 foreach ($array as $field_name => $value) {
254 if ($this->
wakeUp($field_name, $value) === NULL) {
255 $this->{$field_name} = $value;
257 $this->{$field_name} = $this->
wakeUp($field_name, $value);
272 public function sleep($field_name) {
283 public function wakeUp($field_name, $field_value) {
303 $field_name = $field->getName();
304 if ($this->
sleep($field_name) === NULL) {
305 $data[$field_name] = array( $field->getFieldType(), $this->{$field_name} );
307 $data[$field_name] = array( $field->getFieldType(), $this->
sleep($field_name) );
328 $class = get_called_class();
343 return self::getCalledClass()->installDatabase();
364 return self::getCalledClass()->getArConnector()->renameField(self::getCalledClass(), $old_name, $new_name);
372 return self::getCalledClass()->getArConnector()->checkTableExists(self::getCalledClass());
382 return self::getCalledClass()->getArConnector()->checkFieldExists(self::getCalledClass(), $field_name);
392 return self::getCalledClass()->getArConnector()->removeField(self::getCalledClass(), $field_name);
403 $fields[$field->getName()] = $field->getAttributesForConnector();
417 if (! self::tableExists()) {
418 self::getCalledClass()->installDatabase();
423 return self::getCalledClass()->getArConnector()->updateDatabase(self::getCalledClass());
431 return self::getCalledClass()->getArConnector()->resetDatabase(self::getCalledClass());
439 return self::getCalledClass()->getArConnector()->truncateDatabase(self::getCalledClass());
447 return self::truncateDB();
454 if (! $this->getId()) {
483 public function copy($new_id = 0) {
484 if (self::where(array( $this->
getArFieldList()->getPrimaryFieldName() => $new_id ))->hasSets()) {
487 $new_obj = clone($this);
488 $new_obj->setPrimaryFieldValue($new_id);
502 } elseif (
count(
$records) == 0 AND $this->ar_safe_read ==
false) {
503 $this->is_new =
true;
507 if ($this->
wakeUp($k, $rec->{$k}) === NULL) {
508 $this->{$k} = $rec->{$k};
510 $this->{$k} = $this->
wakeUp($k, $rec->{$k});
525 public function delete() {
550 $srModelObjectList->additionalParams($additional_params);
552 return $srModelObjectList;
562 public static function find($primary_key, array $add_constructor_args = array()) {
567 $class_name = get_called_class();
569 $obj = arFactory::getInstance($class_name, $primary_key, $add_constructor_args);
570 $obj->storeObjectToCache();
596 public static function findOrFail($primary_key, array $add_constructor_args = array()) {
597 $obj = self::find($primary_key, $add_constructor_args);
615 $obj = self::find($primary_key, $add_constructor_args);
619 $class_name = get_called_class();
620 $obj = arFactory::getInstance($class_name, 0, $add_constructor_args);
621 $obj->setPrimaryFieldValue($primary_key);
623 $obj->storeObjectToCache();
636 public static function where($where, $operator = NULL) {
638 $srModelObjectList->where($where, $operator);
640 return $srModelObjectList;
653 public static function innerjoinAR(
ActiveRecord $ar, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
667 public static function innerjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
670 return $srModelObjectList->innerjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
683 public static function leftjoin($tablename, $on_this, $on_external, $fields = array(
'*' ), $operator =
'=', $both_external =
false) {
686 return $srModelObjectList->leftjoin($tablename, $on_this, $on_external, $fields, $operator, $both_external);
696 public static function orderBy($orderBy, $orderDirection =
'ASC') {
698 $srModelObjectList->orderBy($orderBy, $orderDirection);
700 return $srModelObjectList;
709 public static function dateFormat($date_format =
'd.m.Y - H:i:s') {
711 $srModelObjectList->dateFormat($date_format);
713 return $srModelObjectList;
723 public static function limit($start, $end) {
725 $srModelObjectList->limit($start, $end);
727 return $srModelObjectList;
737 return $srModelObjectList->affectedRows();
745 return self::affectedRows();
752 public static function get() {
755 return $srModelObjectList->get();
765 return $srModelObjectList->debug();
775 return $srModelObjectList->first();
785 return $srModelObjectList;
792 public static function last() {
795 return $srModelObjectList->last();
806 return $srModelObjectList->getFirstFromLastQuery();
818 return $srModelObjectList->connector($connector);
827 public static function raw($set_raw =
true) {
830 return $srModelObjectList->raw($set_raw);
840 public static function getArray($key = NULL, $values = NULL) {
843 return $srModelObjectList->getArray($key, $values);
855 public function __call($name, $arguments) {
857 if (preg_match(
"/get([a-zA-Z]*)/u", $name, $matches) AND
count($arguments) == 0) {
858 return $this->{self::fromCamelCase($matches[1])};
861 if (preg_match(
"/set([a-zA-Z]*)/u", $name, $matches) AND
count($arguments) == 1) {
862 $this->{self::fromCamelCase($matches[1])} = $arguments[0];
864 if (preg_match(
"/findBy([a-zA-Z]*)/u", $name, $matches) AND
count($arguments) == 1) {
865 return self::where(array( self::fromCamelCase($matches[1]) => $arguments[0] ))->getFirst();
876 public static function _toCamelCase($str, $capitalise_first_char =
false) {
877 if ($capitalise_first_char) {
878 $str[0] = strtoupper($str[0]);
880 $func = create_function(
'$c',
'return strtoupper($c[1]);');
882 return preg_replace_callback(
'/_([a-z])/', $func, $str);
892 $str[0] = strtolower($str[0]);
893 $func = create_function(
'$c',
'return "_" . strtolower($c[1]);');
895 return preg_replace_callback(
'/([A-Z])/', $func, $str);
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 register(ActiveRecord $ar, arConnector $connector)
static purge(ActiveRecord $object)
static returnDbTableName()
__call($name, $arguments)
static fromCamelCase($str)
static dateFormat($date_format='d.m.Y - H:i:s')
static raw($set_raw=true)
const COPY_DESTINATION_ID_EXISTS
setPrimaryFieldValue($value)
__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()
static where($where, $operator=NULL)
__getConvertedDateFieldsAsArray($format=NULL)
wakeUp($field_name, $field_value)
static isCached($class, $id)
const ACTIVE_RECORD_VERSION
static findOrGetInstance($primary_key, array $add_constructor_args=array())
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 getArray($key=NULL, $values=NULL)
static additionalParams(array $additional_params)
__construct($primary_key=0, arConnector $connector=NULL)
static _toCamelCase($str, $capitalise_first_char=false)