public static function read($params, &$totalCount = NULL) { $returnArray = array(); // Connect to database $mysqli = FoodAppDatabase::connect(); // Build SELECT query $queryString = "SELECT DISTINCT owner, category_two FROM food WHERE 1"; // Handle filter parameter if (isset($params["filter"]) && $params["filter"][0]["property"] == "owner") { $queryString .= " AND owner='" . $mysqli->real_escape_string($params["filter"][0]["value"]) . "'"; } $queryString .= " AND category_one='meal'"; // Handle sort $queryString .= " ORDER BY category_two ASC"; // Run query if ($result = $mysqli->query($queryString)) { $iRow = 0; while ($row = $result->fetch_assoc()) { if ($row["owner"] && $row["category_two"]) { $returnArray[$iRow] = array(); $returnArray[$iRow]["owner"] = $row["owner"]; $returnArray[$iRow]["name"] = $row["category_two"]; $iRow++; } } } else { throw new Exception($mysqli->error); } return $returnArray; }
public static function copy($params) { if (!isset($params["id"])) { throw new Exception("Missing id parameter"); } // Connect to database $mysqli = FoodAppDatabase::connect(); // Copy FOOD table record $foodFields = implode(",", array_slice(static::$fieldMap, 1)); $queryString = "INSERT into food (" . $foodFields . ") "; $queryString .= "SELECT " . $foodFields . " FROM food "; $queryString .= "WHERE id='" . $params["id"] . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } $newFoodId = $mysqli->insert_id; // Copy Reviews Review::copy($params["id"], $newFoodId); // Copy Components FoodComponent::copy($params["id"], $newFoodId); // Copy Recipe Steps RecipeStep::copy($params["id"], $newFoodId); // Copy Complements FoodComplement::copy($params["id"], $newFoodId); // Copy Alternates FoodAlternate::copy($params["id"], $newFoodId); }
public static function connectPDO() { if (self::$pdo !== NULL) { return self::$pdo; } $dsn = "mysql:host=" . DATABASE_HOST . ";dbname=" . DATABASE_DATABASE; self::$pdo = new PDO($dsn, DATABASE_USER, DATABASE_PASSWORD, array(PDO::ATTR_PERSISTENT => true)); return self::$pdo; }
/** * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection */ public final function getConnection() { if ($this->connection !== NULL) { return $this->connection; } $pdo = FoodAppDatabase::connectPDO(); $this->connection = $this->createDefaultDBConnection($pdo, FoodAppDatabase::getDatabase()); return $this->connection; }
public static function copy($foodId, $newFoodId) { // Connect to database $mysqli = FoodAppDatabase::connect(); $queryString = "INSERT into review (food_id, review_date, notes) "; $queryString .= "SELECT '" . $newFoodId . "', review_date, notes FROM review "; $queryString .= "WHERE food_id='" . $foodId . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } }
public static function copy($foodId, $newFoodId) { // Connect to database $mysqli = FoodAppDatabase::connect(); $queryString = "INSERT into food_component (food_id, component_id, quantity) "; $queryString .= "SELECT '" . $newFoodId . "', component_id, quantity FROM food_component "; $queryString .= "WHERE food_id='" . $foodId . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } }
public static function copyWithinMealPlan($id) { // Connect to database $mysqli = FoodAppDatabase::connect(); $queryString = "INSERT into meal_plan_component (meal_plan_id, food_id, quantity, meal_time_description, day, meal_time) "; $queryString .= "SELECT meal_plan_id, food_id, quantity, meal_time_description, day+1, meal_time FROM meal_plan_component "; $queryString .= "WHERE id='" . $id . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } // Return id of new record return $mysqli->insert_id; }
function clearTables() { $tables = array("food", "food_component", "recipe_step", "user", "review", "meal_plan", "meal_plan_component"); // Connect to database $mysqli = FoodAppDatabase::connect(); foreach ($tables as $table) { // Build DELETE query $queryString = "DELETE FROM " . $table; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } } echo "Table data cleared\n"; }
public static function copy($params) { if (!isset($params["id"])) { throw new Exception("Missing id parameter"); } // Connect to database $mysqli = FoodAppDatabase::connect(); // Copy meal_plan table record $mealFields = implode(",", array_slice(static::$fieldMap, 1)); $queryString = "INSERT into meal_plan (" . $mealFields . ") "; $queryString .= "SELECT " . $mealFields . " FROM meal_plan "; $queryString .= "WHERE id='" . $params["id"] . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } $newMealId = $mysqli->insert_id; // Copy Components MealPlanComponent::copy($params["id"], $newMealId); }
public static function copy($foodId, $newFoodId) { // Get current set of complements for food $params = array("filter" => array(array("property" => "foodId", "value" => $foodId))); $complements = self::read($params); // Add them to new food if (count($complements) > 0) { // Connect to database $mysqli = FoodAppDatabase::connect(); $queryString = "INSERT into food_complement (food_id, complement_id) VALUES "; foreach ($complements as $row) { $queryString .= "("; $queryString .= "'" . $newFoodId . "', '" . $row["complementId"] . "'"; $queryString .= "),"; } $queryString = rtrim($queryString, ","); // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } } }
public static function copy($foodId, $newFoodId) { // Connect to database $mysqli = FoodAppDatabase::connect(); $queryString = "INSERT into recipe_step (food_id, step_number, description) "; $queryString .= "SELECT '" . $newFoodId . "', step_number, description FROM recipe_step "; $queryString .= "WHERE food_id='" . $foodId . "'"; // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } }
public static function destroy($params) { if (!isset($params["data"]) || count($params) == 0) { return array(); } // Connect to database $mysqli = FoodAppDatabase::connect(); // Build DELETE query $queryString = "DELETE FROM " . static::$table; if (count($params["data"])) { $queryString .= " WHERE "; $first = true; foreach ($params["data"] as $param) { if ($first) { $first = false; } else { $queryString .= " OR "; } $id = $param[static::$idField]; $queryString .= static::$idFieldDB . "='" . $id . "'"; } } // Run query if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } return array(); }
private static function checkRecentItemPresent($params) { if (!isset($params["data"]) || count($params["data"]) == 0) { return false; } $user = $params["data"][0]["user"]; $foodId = $params["data"][0]["foodId"]; $mealPlanId = $params["data"][0]["mealPlanId"]; // Check if item is present $mysqli = FoodAppDatabase::connect(); $queryString = "SELECT * from recent_item WHERE user='******'"; if ($foodId > 0) { $queryString .= " AND food_id='" . $foodId . "'"; } else { $queryString .= " AND meal_plan_id='" . $mealPlanId . "'"; } if ($result = $mysqli->query($queryString)) { if ($row = $result->fetch_assoc()) { // Item found - update the time for item $id = $row["id"]; $queryString = "UPDATE recent_item SET time=NOW() WHERE id='" . $id . "'"; if (!$mysqli->query($queryString)) { throw new Exception($mysqli->error); } return true; } } else { throw new Exception($mysqli->error); } return false; }