This class provides ORM and database query builder functions.
The get the full power of your model, we show you how to write your own model. See the example below.
<?php
namespace Model;
class Articles extends \Model
{
protected $table_name = 'articles';
protected $primary_key = 'id';
protected $properties = array(
'name',
'status',
'created',
'updated',
);
Thats all! Now lets continue work with your model in following examples :)
Here is a list of available public methods for this class:
The find method can be used in thre ways: find a specific id, find first/last or all entries with conditions.
Static | Yes | |||
Parameters | Parameter | Type | Default | Description |
$id | mixed | 'all' | id (primary key) or string with following values: 'first', 'last', 'all' | |
$options | array | Options array where you can add 'where' or 'group_by' options. |
<?php
use \Model\Articles;
// if you know the id, you can find by id
$model = Articles::find(1);
// find first entry where access true
$model = Articles::find('first', array(
'where' => array(
'status' => 'true'
)
));
// find all
$model = Articles::find('all');
Return the table name of given Model.
Static | No |
<?php
use \Model\Articles;
$model = new Articles();
echo $model->table();
// Output: articles
Return the primary key of given Model.
Static | No |
<?php
use \Model\Articles;
$model = new Articles();
echo $model->primary_key();
// Output: id
Return the primary key of given Model.
Static | No |
<?php
use \Model\Articles;
$model = new Articles();
var_dump($model->properties());
// Output
array(4)
{
[0]=> string(4) "name"
[1]=> string(6) "status"
[2]=> string(7) "created"
[3]=> string(7) "updated"
}
Query builder: The select method appends columns to the select statement.
Static | No | |||
Parameters | Parameter | Type | Default | Description |
$columns | Array or String | null | Name of column or array with columns. |
<?php
use \Model\Articles;
$model = new Articles();
// Select column 'name'
$model->select('name')->from('articles');
// SELECT `name` FROM `articles`
// Select columns 'name' and 'status'
$model->select(array(
'name',
'status'
)
)->from('articles');
// SELECT `name`, `status` FROM `articles`
Query builder: The distinct method sets whether to select distinct values.
Static | No | |||
Parameters | Parameter | Type | Default | Description |
$value | boolean | Set to false if you don't want to distinct. |
<?php
use \Model\Articles;
$model = new Articles();
// Select distcint column 'name'
$model->select('name')->from('articles')->distinct();
// SELECT DISTINCT `name` FROM `articles`
Query builder: Set the table name.
If you wrote your own model, the $table_name property is set in your model - just call select() without parameter.
Static | No | |||
Parameters | Parameter | Type | Default | Description |
$table_name | String | Name table name. |
<?php
use \Model\Articles;
$model = new Articles();
$model->select()->from('articles');
// SELECT * FROM `articles`
$model->select()->from();
// SELECT * FROM `articles`
Query builder: This function appends the table to JOIN from.
Static | No | |||
Parameters | Parameter | Type | Default | Description |
$table | String | Name of table. | ||
$direction | String | LEFT | JOIN direction. |
<?php
use \Model\Articles;
$model = new Articles();
$model->select()->from()->join('users');
// SELECT * FROM `articles` LEFT JOIN `users`
Query builder: This function appends the table to join on.
Important notice: You have to write the tablename and the row separated with a dot for each column like "articles.group_id".
Static | No | |||
Parameters | Parameter | Type | Default | Description |
$table | String | Name of table. | ||
$direction | String | LEFT | JOIN direction. |
<?php
use \Model\Articles;
$model = new Articles();
$model->select()->from()->join('article_groups')->on('articles.group_id', 'article_groups.id');
// SELECT * FROM `articles` LEFT JOIN `article_groups` ON `arcticles`.`group_id` = `article_groups`.`id`
Query builder: This function set the WHERE / AND condition.
Hint: If you want only to set a simple = condition, you can use only two parameters.
Query builder: This function set the limit and offset.
Query builder: This function set ORDER BY.
Query builder: This function set GROUP BY.
Query builder: This function enable of disable cache.
Query builder: This function execute the compiled statement.
Query builder: This function return the result as array.
Query builder: This function return the result as object.
Query builder: This method build the full select query.
Query builder: This function set property for INSERT or UPDATE data.
Query builder: This function INSERT or UPDATE data into database.
Query builder: This function INSERT data into database.
Query builder: This function UPDATE data in database.
Query builder: This function DELETE data from database.
This function prepare data for INSERT.
This function prepare data for INSERT.