Ocs-server/Gfx4/Models: Difference between revisions

From KDE Community Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
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::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 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()''':
Returns all fields contained in table structure
Returns all fields contained in table structure
----------
----------
* '''EModel::insert($allowed_fields=array())''':
* '''EModel::insert($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.
Being $field and associative array $key => $value with the correct data.
Example:
Example:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
<?php
<?php
//let's assume the page will receive 2 GET parameters, one with key 'name' and one with key 'password'
//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'])){
$name = EHeader::db_get('name');
    $e = new EModel('users'); //'users' is the name of the table that contains a column called 'name' and one called 'password'
$password = EHeader::db_get('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
$e = new EModel('users'); //'users' is the name of the table that contains a column called 'name' and one called 'password'
    //and matching but not allowed
$e->insert(array('name' => $name, 'password' => $password));
}
?>
?>
</syntaxhighlight>
</syntaxhighlight>
----------
----------
* '''EModel::count($field="id", $where="")''':
* '''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.
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:
Example:
Line 58: Line 66:
</syntaxhighlight>
</syntaxhighlight>
----------
----------
* '''EModel::is_there($field="", $where="")''':
* '''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.
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:
Example:
Line 70: Line 78:
</syntaxhighlight>
</syntaxhighlight>
----------
----------
* '''EModel::delete($where="", $howmany="")''':
* '''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.
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:
Example:
Line 82: Line 90:
</syntaxhighlight>
</syntaxhighlight>
----------
----------
* '''EModel::update($where="", $allowed_fields=array())''':
* '''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.
The same as EModel::insert(), just for updating fields. For security reasons is good practice to specify $allowed_fields() with only allowed fields.
Example:
Example:

Latest revision as of 10:33, 23 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::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
}
?>