Skip to content

Commit

Permalink
page model debug
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Feb 20, 2023
1 parent 07ba596 commit 41e7e79
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app/models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,27 @@ function schema( $schema=array() ){
if( !array_key_exists($key, $schema) ) $schema[$key] = "";
}
}
} else {
// create table
$keys = implode(", ", array_keys( $schema ) );
// FIX: The id needs to be setup as autoincrement
$keys = str_replace("id,", "id INTEGER PRIMARY KEY ASC,", $keys);
$page->create_table("pages", $keys );
}

foreach( $schema as $key => $value ){
if( !array_key_exists($key, $this->rs) ) $this->rs[$key] = null;
// add the column if necessary
$sql = "SELECT COUNT(*) AS OK FROM pragma_table_info('pages') WHERE name='$key'";
$results = $dbh->prepare( $sql );
$results->execute();
$column = $results->fetch(PDO::FETCH_ASSOC);
//
if( $column["OK"] == 0 ){
$sql = "ALTER TABLE pages ADD COLUMN ". $key;
$results = $dbh->prepare($sql);
if( $results ) $results->execute();
}
}

// save schema in the global namespace
Expand Down Expand Up @@ -121,21 +138,27 @@ static function register($id, $key=false, $value="") {
$results->execute();
$table = $results->fetch(PDO::FETCH_ASSOC);

// then check if the table exists
// [DEPRECATED] then check if the table exists
if(!is_array($table)){
$keys = implode(", ", $columns);
// FIX: The id needs to be setup as autoincrement
$keys = str_replace("id,", "id INTEGER PRIMARY KEY ASC,", $keys);
$page->create_table("pages", $keys );
//$page->create_table("pages", "id INTEGER PRIMARY KEY ASC, title, content, path, date, tags, template");
}

// [DEPRECATED] add the column if necessary
$sql = "SELECT COUNT(*) AS OK FROM pragma_table_info('pages') WHERE name='$key'";
$results = $dbh->prepare($sql);
$results->execute();
$column = $results->fetch(PDO::FETCH_ASSOC);

// add the column if necessary
if( array_key_exists("pages", $GLOBALS['db_schema']) && is_array($columns) && !in_array($key, $columns) ){
// if( array_key_exists("pages", $GLOBALS['db_schema']) && is_array($columns) && !in_array($key, $columns) ){
if( $column["OK"] == 0 ){
$sql = "ALTER TABLE pages ADD COLUMN ". $key;
$results = $dbh->prepare($sql);
if( $results ) $results->execute(); // there's a case where the column may already exist, in which case this will be false...
array_push( $columns, $key );
array_push( $columns, $key ); // obsolete?
}

// get existing page (again?)
Expand Down

0 comments on commit 41e7e79

Please sign in to comment.