public function testConnection()
 {
     $config1 = $this->config['DBconfig'];
     $Db1 = new DbModel($config1);
     $this->assertEquals(true, $Db1->CheckConnection());
     $config2 = $this->config['DBconfig'];
     $config2['username'] = "******";
     @($Db2 = new DbModel($config2));
     $this->assertEquals(false, $Db2->CheckConnection());
 }
Exemple #2
0
 public static function getInstance()
 {
     if (!self::$_instance) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 static function getSettings()
 {
     $db = DbModel::getInstance();
     $db->query('SELECT * from settings');
     $db->stmt->execute();
     return $db->single();
 }
Exemple #4
0
 function __construct()
 {
     //there is a chance these things will have been already created with another db instance, so check
     $this->CrudModel = new CrudModel($this);
     $this->DbModel = DbModel::init($this->db->name, $this->db);
     $columns = $this->CrudModel->columns($this->model['table']);
     $keys = array_keys($columns);
     $this->defaultConcernData = array();
     foreach ($keys as $key) {
         $this->defaultConcernData[$key] = true;
     }
     krsort($this->crud);
     //reverse key sorted so specific rules outweight combined rules
     Hook::runWithReferences('newCrudPage', $this);
 }
Exemple #5
0
 public function get(array $where = [])
 {
     $this->isNew = false;
     $condition = DbModel::buildWhere($where);
     $query = "SELECT * FROM {$this->tableName} " . ($condition != null ? 'WHERE ' . $condition : "");
     $datastore = new DbModelStore();
     $itr = 0;
     $stmt = DbConnection::getDefault()->prepare($query);
     $stmt->execute();
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $classname = get_class($this);
         $model = new $classname();
         foreach ($this->tableMap as $prop) {
             $setter = "set" . ucfirst($prop->getModelPropertyName());
             $model->{$setter}($row[$prop->getDbPropertyName()]);
         }
         $model->isNew = false;
         $datastore->Add($itr++, $model);
     }
     return $datastore;
 }
Exemple #6
0
 /**
  * Finds Statements that match a pattern in the default Graph. The argument may contain
  * wildcards.
  *
  * @param Resource or null
  * @param Resource or null
  * @param Resource or null
  * @return IteratorFindQuadsDb
  */
 function &findInDefaultGraph($subject, $predicate, $object)
 {
     $defaultGraphID = (int) $this->dbConnection->GetOne("SELECT models.modelID FROM datasets, models WHERE datasets.datasetName ='" . $this->setName . "' AND datasets.defaultModelUri = models.modelURI");
     // static part of the sql statement
     $sql = "SELECT subject, predicate, object, l_language, l_datatype, subject_is, object_is\n          \t\tFROM statements\n           \t\tWHERE modelID ='{$defaultGraphID}'";
     // dynamic part of the sql statement
     $sql .= DbModel::_createDynSqlPart_SPO($subject, $predicate, $object);
     // execute the query
     $recordSet =& $this->dbConnection->execute($sql);
     $it = new IteratorFindQuadsDb($recordSet, $this, true);
     return $it;
 }
Exemple #7
0
<?php 
require_once 'resources.php';
$model = null;
$request = 0;
//If this is not empty, something was requested.
if (isset($_GET['id'])) {
    $request = $_GET['id'];
    //A valid request was received.
    if (is_numeric($request) || strtolower($request == "all")) {
        //Create an instance of the DAO that will access the database.
        $model = new DbModel(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if (is_numeric($request)) {
            echo $model->get_item($request);
        } else {
            echo $model->get_all_items();
        }
    }
} else {
    echo "Get single item: http://localhost/Shop/index.php?id=x  (Where x is the 'id' number representing a row in the database).\n        <p>\n        Get all items:http://localhost/Shop/index.php?id=all";
}
Exemple #8
0
        throw new DbException('Database model ' . get_called_class() . ': can\'t find field `' . $field . '` in the schema');
    }
    public function __set($field, $value)
    {
        debug('DbModel::__set(' . $field . ', ' . var_export($value, true) . ')');
        $schema = self::get_schema();
        if (!array_key_exists($field, $schema)) {
            throw new DbException('Database model ' . get_called_class() . ': can\'t find field `' . $field . '` in the schema');
        }
        if ($field == $this->get_primary_key()) {
            throw new DbException('Can\'t assign a value to the primary key of the object');
        }
        if (!$schema[$field]->can_assign_value($value)) {
            warning('Can\'t assign this value to ' . get_called_class() . '::' . $field . ':');
            html_var_dump($value);
            throw new InvalidValueException('Can\'t assign this value to ' . get_called_class() . '::' . $field . ':');
        }
        $this->fields[$field] = $value;
        return true;
    }
}
DbModel::$connection = new DbConnection();
class DbException extends Exception
{
}
class InvalidValueException extends DbException
{
}
class DbConstraintsException extends DbException
{
}
 public static function delete($id)
 {
     $dbc = new DbModel();
     $dbc->delete($id);
     $dbc = null;
 }
Exemple #10
0
 static function getAllUsersPerPage($offset, $per_page)
 {
     $db = DbModel::getInstance();
     $db->query('SELECT * from users WHERE disabled != 1 LIMIT :per_page OFFSET :offset');
     $db->bind(':per_page', $per_page);
     $db->bind(':offset', $offset);
     $db->stmt->execute();
     return $db->resultSet();
 }
Exemple #11
0
 public function deleteTopPost($toppostid)
 {
     $db = DbModel::getInstance();
     // First delete all childs of this toppost
     $db->query('DELETE FROM posts WHERE post_relation_id = :id ');
     $db->bind(':id', $toppostid);
     $db->stmt->execute();
     // Now delete the toppost itself
     $db->query('DELETE FROM posts WHERE id = :id ');
     $db->bind(':id', $toppostid);
     $db->stmt->execute();
     if ($db->rowCount() === 1) {
         // Remove the views file
         $filename = BASE_URI . 'views/' . $toppostid . '.txt';
         if (unlink($filename)) {
             // Views file is deleted as well as the toppost
             return true;
         } else {
             // Views file is not deleted (toppost also)
             return false;
         }
     } else {
         // Views file is not deleted (toppost also)
         return false;
     }
 }
 static function countCategories()
 {
     $db = DbModel::getInstance();
     $db->query('SELECT COUNT(*) FROM categories');
     $db->stmt->execute();
     $result = $db->single();
     return $result['COUNT(*)'];
 }