/** * Takes a row of data or a primary key and makes a hash from the primary key * * @internal * * @param fActiveRecord|array|string|int $record An fActiveRecord object, an array of the records data, an array of primary key data or a scalar primary key value * @param string $class The class name, if $record isn't an fActiveRecord * @return string|NULL A hash of the record's primary key value or NULL if the record doesn't exist yet */ public static function hash($record, $class = NULL) { if ($record instanceof fActiveRecord && !$record->exists()) { return NULL; } if ($class === NULL) { if (!$record instanceof fActiveRecord) { throw new fProgrammerException('The class of the record must be provided if the record specified is not an instance of fActiveRecord'); } $class = get_class($record); } $schema = fORMSchema::retrieve($class); $table = fORM::tablize($class); $pk_columns = $schema->getKeys($table, 'primary'); // Build an array of just the primary key data $pk_data = array(); foreach ($pk_columns as $pk_column) { if ($record instanceof fActiveRecord) { $value = self::hasOld($record->old_values, $pk_column) ? self::retrieveOld($record->old_values, $pk_column) : $record->values[$pk_column]; } elseif (is_array($record)) { $value = $record[$pk_column]; } else { $value = $record; } $pk_data[$pk_column] = fORM::scalarize($class, $pk_column, $value); if (is_numeric($pk_data[$pk_column]) || is_object($pk_data[$pk_column])) { $pk_data[$pk_column] = (string) $pk_data[$pk_column]; } } return md5(serialize($pk_data)); }
public function exists() { if (isset($this->columnAliases['id']) && !is_null(parent::get($this->columnAliases['id']))) { return true; } return parent::exists(); }
/** * Sets the appropriate column values to the date the object was created (for new records) * * @internal * * @param fActiveRecord $object The fActiveRecord instance * @param array &$values The current values * @param array &$old_values The old values * @param array &$related_records Any records related to this record * @param array &$cache The cache array for the record * @return void */ public static function setDateCreated($object, &$values, &$old_values, &$related_records, &$cache) { if ($object->exists()) { return; } $class = get_class($object); foreach (self::$date_created_columns[$class] as $column => $enabled) { fActiveRecord::assign($values, $old_values, $column, fORM::objectify($class, $column, date('Y-m-d H:i:s'))); // If the column has a corresponding timezone column, set that too if (isset(self::$timestamp_columns[$class][$column])) { fActiveRecord::assign($values, $old_values, self::$timestamp_columns[$class][$column], fTimestamp::getDefaultTimezone()); } } }
/** * Sets the appropriate column values to a random string if the object is new * * @internal * * @param fActiveRecord $object The fActiveRecord instance * @param array &$values The current values * @param array &$old_values The old values * @param array &$related_records Any records related to this record * @param array &$cache The cache array for the record * @return string The formatted link */ public static function setRandomStrings($object, &$values, &$old_values, &$related_records, &$cache) { if ($object->exists()) { return; } $class = get_class($object); $table = fORM::tablize($class); foreach (self::$random_columns[$class] as $column => $settings) { if (fActiveRecord::hasOld($old_values, $column) && $values[$column]) { continue; } self::generate($object, $values, $old_values, $related_records, $cache, 'generate' . fGrammar::camelize($column, TRUE), array()); } }