edit

Skytells Framework Models


What is a Models?

Models are PHP classes that are designed to work with information in your database. For example, let’s say you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and retrieve your blog data. Here is an example of what such a model class might look like:

Note

Models are optionally available for those who want to use a more traditional MVC approach.


Checking Database Configuration

By using Skytells Framework Models, You MUST configure your database from the databases configuration file, if you do not have any idea about how to configure your database file
Please Click here to learn how.

Creating Models

All Models must be stored in this folder.

1
Application/Resources/Models

Create a file inside the mentioned path to be like :

1
Application/Resources/Models/MyModel.php

Put with the following code in your model.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
use Skytells\Events\Dispatcher;
use Skytells\Container\Container;
use Skytells\Database\Capsule\Manager as Capsule;

Class HomeModel extends Model {
  function __construct($Ref = '') {
     parent::__construct();

      // Connecting to the Selected Database Group.
       $this->Connect('Default') // Connect to db.1

  }
}

Now noticed that we've used the following code in construct() function to connect our database.

1
2
<?php
$this->Connect('Default') // Connect to db.1

Database Structure

Now lets create our Table and data inside our database.

Open phpMyAdmin and perform this command.

1
2
3
4
5
6
7
CREATE TABLE Users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)

For now we've created a DB Table which contains:

  • id (int)
  • firstname (VARCHAR)
  • lastname (VARCHAR)
  • email (VARCHAR)
  • reg_date (TIMESTAMP)

Model Methods

Now lets create a method with name getUsers in our model containing this code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
use Skytells\Events\Dispatcher;
use Skytells\Container\Container;
use Skytells\Database\Capsule\Manager as Capsule;

Class HomeModel extends Model {
  function __construct($Ref = '') {
     parent::__construct();
     $this->Connect('Default') // Connect to db.1
  }

  function getUsers() {
    return $this->SQLManager['Default']->get('users');
    // Outputs Users Table Contents from Database Default
  }
}

Warning

SQL Manager Engine is loaded in model by default, Learn more about this engine from the drivers documentations.

You can use many drivers and ORMs in models, we will talk about it later.

Of course you can use a RAW DB as well, by using this code :

1
2
3
4
5
<?php
 function getUsers() {
    return $this->db['Default']->query('SELECT * FROM Users');
    // Outputs Users Table Contents from Database Default
 }

OR by Skytells Capsule (Requires Skytells to be TRUE from the db's config)

1
2
3
4
5
<?php
 function getUsers() {
   return $this->Capsule['Default']->table('users')->where('id', '=', (int)$ID)->get();
    // Outputs Users Table Contents from Database Default
 }

Load Models in Controllers

Now Get Back to your controller and load the model.

Open your controller from :

1
Application/Resources/Controllers/MyController.php

Put the following code to load the model.

1
2
<?php
$this->load->model('HomeModel', $this);

Loader Documentations

Using Skytells Loader is too easy, You can see the full documentations for the loader if you want to know its full functionality.

Once loaded, you will access your model methods using an object with the same name as your class:

1
2
<?
$this->model_name->method();

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method:

1
2
<?
$this->load->model('model_name', $this, false, 'newname');
1
2
<?
$this->newname->method();

Now after putting the loader code in the construct() function,
It should look like this :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
use Skytells\Core;
use Skytells\Runtime\Console;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
      $this->load->model('HomeModel', $this);
    }
}

Now lets call the model function containing the DB outputs and dump it in browser.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
use Skytells\Core;
use Skytells\Runtime\Console;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
      $this->load->model('HomeModel', $this);

      $Users = $this->HomeModel->getUsers();
    }
}

Now we have an Array in name of $Users containing the model's outputs.

You can dump it easily by using d() function.

Example :

1
2
3
4
5
6
7
<?php
function __construct($Ref = "") {
    parent::__construct();
    $this->load->model('HomeModel', $this);
    $Users = $this->HomeModel->getUsers();
    d($Users);
}