Ocs-server/Gfx4/Models: Difference between revisions

From KDE Community Wiki
No edit summary
No edit summary
Line 25: Line 25:
</syntaxhighlight>
</syntaxhighlight>
------------------
------------------
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 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 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()''':
* '''EModel::fields()''':

Revision as of 13:27, 18 May 2016

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 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($allowed_fields=array()):

Attempts to do an automatic insert in the table. EModel will look into GET / POST parameters passed to the page, and if the name of the parameter is the same of the column in the table, that will have the correct value when inserted. For security reasons every field that will use this "automatic" feature must be inserted in the $allowed_fields array when insert() is called. Example:

<?php
//let's assume the page will receive 2 GET parameters, one with key 'name' and one with key 'password'
if(isset($_GET['name']) and isset($_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', 'password')); //allow both fields
    //now table is inserted with a new element that has correct name and password, also when other fields are present
    //and matching but not allowed
}
?>

  • EModel::count($field="id", $where=""):

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($field="", $where=""):

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($where="", $howmany=""):

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($where="", $allowed_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
}
?>