Ocs-server/Gfx4/Models

From KDE Community Wiki

The EModel class contains various facilities for querying databases and performing standard tasks like SELECT, INSERT, UPDATE and DELETE. So, for example, if you're writing a class that interfaces with the "posts" class in your database, you should create a file name "posts.model.php" in the "model" directory. GFX already knows that there are models there. This is a convention when creating models directly binded to a database table but feel free to create models with custom names. There is no particular restriction on Model's use.

Every model should be name following this syntax <tablename>.model.php in order to be automatically loaded by gfx as a model. Then you can write your code that basically queries the database and returns the correct data, along with all manipulations done.

Think of a Model of an object that has to return data that should already human understandable if seen raw.

This is an example of a basic model class:

<?php
class ArticlesModel extends EModel
{
    public function __construct()
    {
        parent::__construct("articles");
    }
        
    public function getAll()
    {
       $data = $this->find("*");
        return $data;
    }
}
?>

EModel works with conditions that stays still after a query has been executed.

  • EModel::add_condition($c); //example: $model->add_condition('a=1'); $model->find('*');
  • EModel::clear_conditions(); //example: $model->clear_conditions();
  • EModel::set_limit($limit); //$limit being an integer
  • EModel::clear_limit();
  • EModel::set_ascending();
  • EModel::set_descending();
  • EModel::order_by($field=array()); //example: $model->order_by(array('id','name');
  • EModel::clear_ordering();

EModel class wraps many of the operations that can be made to a database. Here is a list of the most useful methods of EModel:

  • EModel::fields():

Returns all fields contained in table structure


  • EModel::insert($fields=array()):

Being $field and associative array $key => $value with the correct data. Example:

<?php
//let's assume the page will receive 2 GET parameters, one with key 'name' and one with key 'password'
$name = EHeader::db_get('name');
$password = EHeader::db_get('password');

$e = new EModel('users'); //'users' is the name of the table that contains a column called 'name' and one called 'password'
$e->insert(array('name' => $name, 'password' => $password));
?>

  • EModel::count($fields=array()):

Performs a COUNT() of a SELECT query. $field is the field to select. Additional parameters can be added at the end of the query with $where parameter. Example:

<?php
    $e = new EModel('users');
    $result = $e->count('name', 'where password=$password');
    //$result contains exactly the number of rows returned, it beings of course an integer
}
?>

  • EModel::is_there($fields=array()):

Checks if a query returns more than a row and returns true or false. $field is the field to select. Additional parameters can be added at the end of the query with $where parameter. Example:

<?php
    $e = new EModel('users');
    $result = $e->is_there('name', 'where password=$password');
    //$result contains true or false, whether the query result has at least one row or not
}
?>

  • EModel::delete():

Wraps a DELETE query and deletes rows following $where conditions to be put at the end of the query. $howmany is used to put a limit to the rows being deleted. Example:

<?php
    $e = new EModel('users');
    $e->delete('password=$password', 1);
    //this time the '''WHERE''' statement hasn't to be manually added to the $where parameter
}
?>

  • EModel::update($fields=array()):

The same as EModel::insert(), just for updating fields. For security reasons is good practice to specify $allowed_fields() with only allowed fields. Example:

<?php
    $e = new EModel('users');
    $e->update('password=$password', array('name'));
    //only name will be updated, even if password is defined
}
?>