ORM

The ORM in Tina4 tries to be as light as possible on coding, the basic form uses the object name to map to the table and assumes the first public variable you declare is the primary key.

ORM is used to map object relations and how they interact with each other, for your app/webpage/system.

Map a Database Table

In Tina4 we map Database tables using an ORM class in the /src/orm directory.

We assume that there is a table “user” in the database. See the migrations section if you have not created the table yet.

<?php

//we need a class extending the ORM

class User extends \Tina4\ORM {
    public $id; //primary key because it is first
    public $name; //some additional data
}

If you want, you can also map table names to classes with more friendly names by specifying the table name. This is particularly useful when you're building plugins by adding Tina4 into existing code.

Also note that I'm explicitly defining the primary key below.

<?php

//we need a class extending the ORM
class Clients extends \Tina4\ORM {
    public $tableName = "tblclients";
    public $primaryKey = "id";

    public $id; //primary key because it is first
    public $name; //some additional data
}

Tina4 is smart in picking up table names that match your class name.

Save a record using the ORM

To save a record/data to the database using ORM, it is required that you extend the \Tina4\ORM class to make the magic happen.

Create your ORM classes and objects in the \src\ORM directory.

Example:

<?php

//we need a class extending the ORM
class User extends \Tina4\ORM { //assumes we have a table user in the database
    public $id; //primary key because it is first
    public $name; //some additional data
}

$user = (new User());
$user->name = "Test Save";
$user->save();


//We want the table to be made for us
class NewTable extends \Tina4\ORM {
    //will be created as newtable in the database
    /**
    *  @var id integer auto_increment
    **/
    public $id; //primary key because it is first
    /**
    *  @var varchar(100) default 'Default Name'
    **/
    public $name; //some additional data
}


$newTable = (new NewTable());
$newTable->name = "Test Save";
$userTable->save();

//How about something else

$newTable = (new NewTable('{"name":"TEST"}'));
$newTable->save();

//Or something else

$fields = ["name" => "Testing"];
$newTable = (new NewTable($fields));
$newTable->save();

//Or something else - request variable should obviously 
//contain fields that match the class declared
$newTable = (new NewTable($_REQUEST));
$newTable->save();

Load a record using the ORM

The load method of ORM allows you to select and update records. It returns false if there are no records matched. As you'll see, you add the where part of a select statement into the load() method.

//Assuming we've got a record for Bob that we want to select
//Let's update the user's name

$user = (new User());
$user->load("name = 'Bob' ");

if ($user != false) //check if we have a record
{
   $user->name = "Bobbey"
}

$user->save();


//Let's do a more elegant load

if (($user = (new User())->load("name = 'Bobbey' "))
{
  //The user exists do some checks here
}
else
{
  //The user does not exist, let's insert it
  $user = (new User()) //create a new record like we did in our first example
  $user->name = "Bobbey";
}

Other methods using the ORM

Tina4 can make use of various types of methods.

Example:

<?php

//Standard
\Tina4\Get::add("/hello/world", function(\Tina4\Response $response, \Tina4\Request $request){  
    return $response("Hello World", HTTP_OK, TEXT_HTML);
});

//Inline Params
\Tina4\Get::add("/hello/world/{id}", function($id, \Tina4\Response $response, \Tina4\Request $request){
    return $response("Hello World {$id}", HTTP_OK, TEXT_HTML);
});

//Other methods you can test
\Tina4\Post::add(...);

\Tina4\Patch::add(...);

\Tina4\Put::add(...);

\Tina4\Delete::add(...);

//You guessed it - It takes every method - GET, POST, DELETE, PUT, PATCH, OPTIONS
\Tina4\Any::add(...);

Reference a table using the ORM

To reference a table in your database using Tina4, all you need to do is declare $tableName as a public variable. Below is an example of the code convention:

Example:

<?php

class Person extends ORM
{
    public $tableName = "person"; //--> here we reference the table name in the database

    //--> here we reference the columns in our table
    public $id;
    public $firstName;
    public $lastName;
}
$person = (new Person());
$person->name = "Test Save";
$person->save();

Ensure duplicates are not created in the database using ORM

To ensure there are no duplicate entries and values created in your database, all you need to do is declare $primarykey name as a public variable.

Example:

<?php

class Person extends ORM
{
    public $tableName = "person"; //--> here we reference the table name in the database
    public $primaryKey = "id"; //--> here we include the primary key SQL constraint

    //--> here we reference the columns in our table
    public $id;
    public $firstName;
    public $lastName;
}
$person = (new Person());
$person->name = "Test Save";
$person->save();

Inline constraints using ORM

You can use make use of constraints within your ORM script.

<?php

class Person extends ORM
{
    public $tableName = "person"; //--> here we reference the table name in the database
    public $primaryKey = "id"; //--> here we include the primary key SQL constraint

    //--> here we reference the columns in our table
    public $id (int not null default);
    public $firstName (varchar 100 not null);
    public $lastName (varchar 100 not null);
}
$person = (new Person());
$person->name = "Test Save";
$person->save();

Take a look at Migrations, Swagger or Saving Local Dates as it can be used with ORM.

Powered by ComboStrap