LightPHPFramework

Model Class

This class provides ORM and database query builder functions.


Write your own model

The get the full power of your model, we show you how to write your own model. See the example below.

Example

<?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 :)


List of public methods

Here is a list of available public methods for this class:


find()

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.
Example

<?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');

	

table()

Return the table name of given Model.

Static No
Example

<?php

use \Model\Articles;

$model = new Articles();

echo $model->table();

// Output: articles

	

primary_key()

Return the primary key of given Model.

Static No
Example

<?php

use \Model\Articles;

$model = new Articles();

echo $model->primary_key();

// Output: id

	

properties()

Return the primary key of given Model.

Static No
Example

<?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"
}

	

select($columns)

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.
Example

<?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`

	

distinct($value)

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.
Example

<?php

use \Model\Articles;

$model = new Articles();

// Select distcint column 'name'
$model->select('name')->from('articles')->distinct();

// SELECT DISTINCT `name` FROM `articles`
		
	

from($table_name)

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.
Example
		
<?php

use \Model\Articles;

$model = new Articles();

$model->select()->from('articles');

// SELECT * FROM `articles`

$model->select()->from();

// SELECT * FROM `articles`
		
	

join($table, $direction)

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.
Example
		
<?php

use \Model\Articles;

$model = new Articles();

$model->select()->from()->join('users');

// SELECT * FROM `articles` LEFT JOIN `users`
		
	

on($column1, $column2)

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.
Example
		
<?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`
		
	

where(($column, $operator, $value)

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.


limit($limit, $offset)

Query builder: This function set the limit and offset.


order_by($column, $direction)

Query builder: This function set ORDER BY.


group_by($group_by)

Query builder: This function set GROUP BY.


cache($option)

Query builder: This function enable of disable cache.


execute()

Query builder: This function execute the compiled statement.


as_array()

Query builder: This function return the result as array.


as_object()

Query builder: This function return the result as object.


compile()

Query builder: This method build the full select query.


set($property, $value)

Query builder: This function set property for INSERT or UPDATE data.


save()

Query builder: This function INSERT or UPDATE data into database.


insert($data)

Query builder: This function INSERT data into database.


update($id, $data)

Query builder: This function UPDATE data in database.


delete($id)

Query builder: This function DELETE data from database.


prepare_insert($array)

This function prepare data for INSERT.


prepare_update($array)

This function prepare data for INSERT.