Models

Models are the place where you perform all the database related tasks. This includes but not limited to insert, edit, delete database values. You need to put all your models in app/models directory.

Model Structure

Naming the Model

As we previously told, Saika uses PSR-4 naming standards for class names. So your model class name should be in PascalCase. So it can be autoloaded when you call the class. You don't need to require or include them manually. As an example, a Model class name could be:

SampleModel.php

Model Code Structure

Unlike most of the other MVC frameworks Saika doesn't have a core Model class. Because Saika uses singleton pattern for database object. So you actually don't need to extend any class when writing your Model class. A sample Model code could be this:

<?php
/**
* A damn simple demo model!
*
*/
class SampleModel
{

    public static function addPerson($name, $age)
    {
        // Get the database instance from DbFactory
        $db = DbFactory::getFactory()->getConn();
        // See? Pure PDO, nothing else!
        $sql = "INSERT INTO `saika_demo` (name, age) VALUES (:name, :age)";
        $query = $db->prepare($sql);
        $params = array(':name' => $name, ':age' => $age);
        return $query->execute($params);
    }
}

Now in your controller you can use this model just by calling SampleModel::addPerson('John Doe', 29);.

Getting the DB connection

Saika uses singleton pattern for database connection. Which only connects to the database when you really need it. To get the database connection inside your model use DbFactory::getFactory()->getConn(); which returns a PDO object. You can set the database details via the config file.