/** * Check if data is duplicated * @return Row */ public function isDuplicate($create = false) { $fields = $this->table->unique(); $dat = array(); if ($create) { // if we add new row foreach ($fields as $field) { if (isset($this->data[$field])) { $dat[$field] = $this->data[$field]; } } } else { foreach ($fields as $field) { if (isset($this->data[$field]) && isset($this->modifiedData[$field])) { $dat[$field] = $this->data[$field]; } } } if (empty($dat)) { return false; } $builder = $this->table->builder(); foreach ($dat as $key => $val) { $builder->where($key, '=', $val); } $row = $this->table->first($builder); return $row->isEmpty() ? false : $row; }
/** * Copy table * @param $db * @param $name * @return Table */ public function copyTable($name, $db = null, $temp = false) { if (!$temp && !$name) { throw new Exception('New table has no name'); } $db = $db ? $db : $this->db(); $name = $name ? $name : 'temp_' . substr(md5(microtime(true) . rand(1, 9999)), 0, 10); $table = new Table(); $table->connection($this->connection()); $table->db($db); $table->name($name); $table->attrs($this->attrs()); $table->rowClass = $this->rowClass; $builder = $table->builder(); $query = $builder->createTableLike($temp, $this->db(), $this->name()); $table->connection()->query($query); $builder = $this->builder(); $builder->columns(array('*')); $query = $builder->insertSelect($table->db(), $table->name()); $this->connection()->query($query); return $table; }