public function create($object) { global $dataStructure; $columns = formatColumnsForPdo($dataStructure['todos']); // Original like: // "INSERT INTO todos (content, parent, indent, position, done) values (:content, :parent, :indent, :position, :done)" $stmt = $this->db->prepare("INSERT INTO todos ({$columns[0]}) values ({$columns[1]})"); self::prepare($stmt, 'todos', $object); $stmt->execute(); $object['id'] = (int) $this->db->lastInsertId(); $db = null; return print_r(json_encode($object)); }
// If the db file doesn't exist create it! // If it exists then carry on our merry way try { if (!file_exists(DATABASE_NAME . ".db")) { $db = new PDO(DATABASE_FILE); // Create tables foreach ($dataStructure as $table => $layout) { // Format table columns for correct PDO statement $rows = ''; foreach ($layout as $name => $type) { $rows .= $name . ' ' . $type . ','; } $db->exec("CREATE TABLE " . $table . " (" . substr($rows, 0, -1) . ")"); } // Create first list $columns = formatColumnsForPdo($dataStructure['lists']); $stmt = $db->prepare("INSERT INTO lists (" . $columns[0] . ") values (" . $columns[1] . ")"); $row = array('id' => 1, 'name' => 'Default List', 'status' => 0, 'position' => 0); API::prepare($stmt, 'lists', $row); $stmt->execute(); // Create first Todo $columns = formatColumnsForPdo($dataStructure['todos']); $stmt = $db->prepare("INSERT INTO todos (" . $columns[0] . ") values (" . $columns[1] . ")"); $row = array('id' => 1, 'name' => 'First Dummy Todo', 'description' => '', 'status' => 0, 'position' => 0, 'parent_id' => 0, 'list_id' => 1); API::prepare($stmt, 'todos', $row); $stmt->execute(); $db = null; } } catch (Exception $e) { die($e->getMessage()); }