Skip to content

Instantly share code, notes, and snippets.

@ojongchul
Created February 27, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ojongchul/abfdb4f45230add64a13c045d5aca69d to your computer and use it in GitHub Desktop.
Save ojongchul/abfdb4f45230add64a13c045d5aca69d to your computer and use it in GitHub Desktop.
PK가 있는 테이블의 row를 clone하는 함수. PK를 포함해 사용하지 않는 key(column)를 제거한 후 INSERT
<?php
public function cloneRow($pk)
{
$SQL = "SELECT * FROM TBL where id = ".$pk;
$origin = $this->db->getResult()[0];
$clone = $origin;
unset($clone['id']); // PK. auto increment
unset($clone['datetime']); // default now()
unset($clone['state']); // have default values
$keys = array_keys($clone);
$vals = array_values($clone);
$string = "'".join("', '", $vals)."'";
$string = str_replace("''", "NULL", $string);
$SQL = "INSERT INTO TBL (".join(", ", $keys) .") VALUES (".$vals_string.") ";
$this->db->query($SQL);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment