Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Last active March 25, 2019 10:53
Show Gist options
  • Save fyulistian/3e65bf4250b2884e08b66c9c9c88ad30 to your computer and use it in GitHub Desktop.
Save fyulistian/3e65bf4250b2884e08b66c9c9c88ad30 to your computer and use it in GitHub Desktop.
Dynamic function for edit table DB (postgresql many to many) by PHP CI
<?php
/**
* -- Many to Many --
* [processing the reference table to communicate with the database]
* @param string $key [pk of the table] *name of primary keys
* @param string $pk [reference pk to main table] *value of primary keys
* @param array $data [data to be executed]
* @param integer $method [execution method] 0 insert, 1 update
* @return boolean [success = true]
*
* OnGoing FUNC
*/
private function __reference_table_processing($key = "", $pk = "", $data = [], $method = 0) {
// parameters for the query
$direct_batch = array("is_batch" => true, "is_direct" => true); // CI models
$permanent = array("is_permanently" => true); // CI models
// total data
$total_data = count($data);
// prepare data
for ($n = 0; $n < $total_data; $n++) {
$data_reference[] = array(
$key => $data[$n], 'source_primary_keys' => $pk
);
}
// check the destination !! get table from pk
switch ($key) {
case "destination_primary_keys":
$table = "destination_table_name";
break;
#
#
#
# .. add for more
default:
return FALSE
break;
}
// on update
if ($method === 1) {
// delete before insert
$condition = array("destination_pk" => $pk);
$this->_dm->set_model($table)->delete($condition, $permanent); // CI models
}
// not empty data on insert
if (!empty($data_reference)) {
// insert !! reference table
$this->_dm->set_model($table)->insert($data_reference, $direct_batch); // CI models
}
// return
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment