Model

Model class

This class is the main Model class

Example

class User extends Model {
 protected $table = 'user';
 protected $primaryKey = 'id';
 protected $fillable = ["fullName", "email"];
}
$user = new User();
$user->fullName = "John Doe";
$user->email = "johndoe@example.com";
$user->save();
$foundUser = User::find($user->getPrimaryValue());
$user2 = User::where("fullName", "John Doe")->limit(1)->orderBy('id')->exec();
package

Default

Methods

_create

_create() : void

Create new Model, Also does INSERT into $table

_update

_update() : void

Update Model, Also does UPDATE into $table

all

all() : \TinyORM\Model
static

Return all Models from database

Example

User::all();

Response

\TinyORM\Model

array2obj

array2obj(array $arr, mixed $obj) : \TinyORM\Model
static

Arguments

$arr

array

$obj

mixed

Response

\TinyORM\Model

create

create(array $data) : \TinyORM\Model
static

Create new Model

Example

User::Create([
 'name' => 'John Doe'
]);

Arguments

$data

array

Response

\TinyORM\Model

delete

delete() : void

Delete this model from database

Example

$user = User::find('2344');
$user->delete();

exec

exec() : array

Execute the SELECT operation on $this->query

Example

User::where('name', '=', 'John Doe')->exec();

Response

array

find

find(string $id) : \TinyORM\Model
static

Find a Modal by its primary Key

Example

// Find user with id = '1234'
$user = User::find('1234');

Arguments

$id

string

Response

\TinyORM\Model

getPrimaryKey

getPrimaryKey() : string

Returns the primary key

Response

string

getPrimaryValue

getPrimaryValue() : string

Returns the value of primary key

Response

string

getSQLQuery

getSQLQuery() : string

Return the SQL query

Example

echo User::where('name', '=', 'John Doe')->getSQLQuery();
// Returns "SELECT FROM User WHERE name = 'John Doe'"

Response

string

getTable

getTable() : string

Returns the table name for this Model

Response

string

limit

limit(integer $limit) : \TinyORM\Model

Add LIMIT in this SELECT operation

Example

User::where('name', '=', 'John Doe')->limit(1)->exec();

Arguments

$limit

integer

Response

\TinyORM\Model

offset

offset(integer $offset) : \TinyORM\Model

Add OFFSET in this SELECT operation

Example

User::where('name', '=', 'John Doe')->offset(10)->exec();

Arguments

$offset

integer

Response

\TinyORM\Model

orderBy

orderBy(string $column, string $dir = 'ASC') : \TinyORM\Model

Add ORDER BY in this SELECT operation

Example

User::where('name', '=', 'John Doe')->orderBy('name', 'DESC')->exec();

Arguments

$column

string

$dir

string

Response

\TinyORM\Model

save

save() : void

This methods does create() or update() depending upon if the primaryValue

Example

$user = new User();
$user->name = 'John Doe';
$user->save();

where

where(string $column, string $operator, string $value = '') : \TinyORM\Model
static

Add where condition in this SELECT operation

Example

User::where('name', '=', 'John Doe')->exec();

Arguments

$column

string

Name of the database table column

$operator

string

SQL operator =, <, >, <> etc

$value

string

Value to the compared with the value in $column and $operator

Response

\TinyORM\Model

Properties

Database table

table : string
var

Database table

Type(s)

string

Primary column name

primaryKey : string
var

Primary column name

Type(s)

string

Query class

query : \TinyORM\Query
var

Query class

Type(s)

\TinyORM\Query

Table columns

fillable : array
var

Table columns

Type(s)

array