Skip to content

Instantly share code, notes, and snippets.

@ubergeekzone
Last active April 10, 2016 23:30
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 ubergeekzone/6b028f88490bf7beceec97a5ed8eca60 to your computer and use it in GitHub Desktop.
Save ubergeekzone/6b028f88490bf7beceec97a5ed8eca60 to your computer and use it in GitHub Desktop.
Custom Many-To-Many MySQL Flow
<?php
$flow = array(
"api_endpoint" => $this->get('from'),
"index" => $this->get("index"), // leave blank for an all query
"where" => array("pointer" => "id",
"table" => "events",
"type" => "MANY_TO_ONE")
);
if($flow['where']['type'] == "MANY_TO_ONE") {
if($flow['api_endpoint'] == $flow['where']['table']) {
$message = array("status" => 200, "data" => array("message" => $flow['where']['type']." not supported bidirectional please use ONE-TO-MANY"));
echo $this->response($message, REST_Controller::HTTP_OK);
}
$where = '';
if($flow['index']) {
$where = " WHERE " .$flow['where']['pointer']. " = '".$flow['index']."'";
}
$endpoint = $this->db->query("SELECT * FROM ". $flow['api_endpoint'] . $where);
if(!$endpoint) {
$message = array("status" => 200, "data" => array("message" => $flow['api_endpoint'] . " is not a valid endpoint."));
echo $this->response($message, REST_Controller::HTTP_OK);
}
if(empty($endpoint->result_array())) {
if($flow['index'] == "") {
$message = array("status" => 200, "data" => array("message" => "no data found"));
} else {
$message = array("status" => 200, "data" => array("message" => "no data found at index " . $flow['index'] . " for pointer " . $flow['where']['pointer']));
}
echo $this->response($message, REST_Controller::HTTP_OK);
}
$i = 0;
foreach ($endpoint->result_array() as $row) {
$link_one = $this->db->query('SELECT * FROM ' . $flow['where']['table'] . " WHERE id" . " = " . $row[$flow['where']['table']."_id"] . " ORDER BY id DESC");
if ($link_one) {
//$message = "No link was found between ".$flow['api_endpoint'] . "_" . $flow['where']['table'];
} else {
$message = array("status" => 200, "data" => array("message" => "No link was found between ".$flow['where']['table']));
echo $this->response($message, REST_Controller::HTTP_OK);
}
$data[] = $row;
$link_i = 0;
foreach($link_one->result_array() as $link_one_row) {
$data[][$flow['where']['table']][] = $link_one_row;
$link_i++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment