The model in Webiik is a standard PHP class. Out of the box, Webiik comes with the component Database that provides a PDO database connection. If you want to use any ORM, add it as a service to the Container and use it inside models.
All models lives in private/code/models
. You can inject dependencies from the Container to any model.
declare(strict_types=1);
namespace Webiik\Model;
use Webiik\Database\Database;
class Model
{
private $db;
public function __construct(Database $database)
{
$this->db = $database;
}
public function get(): array
{
// This is the MySQL example, however, you can configure
// the Database to use any supported language by PDO.
$db = $this->db->connect();
$q = $db->prepare('SELECT * FROM some_table');
$q->execute();
$res = $q->fetchAll(\PDO::FETCH_ASSOC);
return $res ? $res : [];
}
}
⚠️ Don’t forget to make a service from your model. Only then you will be able to inject your model to controllers, middleware, and other models.