Created
February 27, 2020 21:48
-
-
Save ojongchul/abfdb4f45230add64a13c045d5aca69d to your computer and use it in GitHub Desktop.
PK가 있는 테이블의 row를 clone하는 함수. PK를 포함해 사용하지 않는 key(column)를 제거한 후 INSERT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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