/** * Creates a clone of a record * * If the record has an auto incrementing primary key, the primary key will * be erased in the clone. If the primary key is not auto incrementing, * the primary key will be left as-is in the clone. In either situation the * clone will return `FALSE` from the ::exists() method until ::store() is * called. * * @internal * * @return fActiveRecord */ public function __clone() { $class = get_class($this); // Copy values and cache, making sure objects are cloned to prevent reference issues $temp_values = $this->values; $new_values = array(); $this->values =& $new_values; foreach ($temp_values as $column => $value) { $this->values[$column] = fORM::replicate($class, $column, $value); } $temp_cache = $this->cache; $new_cache = array(); $this->cache =& $new_cache; foreach ($temp_cache as $key => $value) { if (is_object($value)) { $this->cache[$key] = clone $value; } else { $this->cache[$key] = $value; } } // Related records are purged $new_related_records = array(); $this->related_records =& $new_related_records; // Old values are changed to look like the record is non-existant $new_old_values = array(); $this->old_values =& $new_old_values; foreach (array_keys($this->values) as $key) { $this->old_values[$key] = array(NULL); } // If we have a single auto incrementing primary key, remove the value $schema = fORMSchema::retrieve($class); $table = fORM::tablize($class); $pk_columns = $schema->getKeys($table, 'primary'); if (sizeof($pk_columns) == 1 && $schema->getColumnInfo($table, $pk_columns[0], 'auto_increment')) { $this->values[$pk_columns[0]] = NULL; unset($this->old_values[$pk_columns[0]]); } }