Introduction SmartAdmin's built-in Database functionalities. Learn more how \Models\Model works.

Documentation

Unknown error (8192): Common\Markdown::blockTable(): Implicitly marking parameter $block as nullable is deprecated, the explicit nullable type must be used instead in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/lib/common/Markdown.php on line 6
[0] in function {closure:/var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/debug.php:6} in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[1] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[2] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322
[3] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/public/php_db_intro.php on line 68
Unknown error (8192): Parsedown::blockSetextHeader(): Implicitly marking parameter $Block as nullable is deprecated, the explicit nullable type must be used instead in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/erusev/parsedown/Parsedown.php on line 715
[0] in function {closure:/var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/debug.php:6} in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[1] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[2] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322
[3] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/lib/common/Markdown.php on line 5
[4] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[5] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322
[6] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/public/php_db_intro.php on line 68
Unknown error (8192): Parsedown::blockTable(): Implicitly marking parameter $Block as nullable is deprecated, the explicit nullable type must be used instead in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/erusev/parsedown/Parsedown.php on line 853
[0] in function {closure:/var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/debug.php:6} in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[1] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[2] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322
[3] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/lib/common/Markdown.php on line 5
[4] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444
[5] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322
[6] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev2.groupe-dmx.fr/public/php_db_intro.php on line 68

PHP Models

A library that allows you to easily create and define your models using PDO.

Installation

To enable Database functionality, include the init.db.php file in your php script. You can also include this in the main init.php file to initiate your database globally.
Follow the database installation guide if you need help configuring your database.

Usage

Model

The \Models\Model class is a parent class that can be inherited to a Model class. Inheriting this class allows you to automatically map the result "row" into your model class (table). This class basically uses the PDO::FETC_INTO style and made it easier for you. Here are the steps to link your table into a class:

Create your model class. For example, a User.php class.

namespace Models;

class User extends Model {
    public function getName() {
        return $this->firstname.' '.$this->lastname;
    }
}

Register your table to your custom Model class.

// somewhere in your init.db.php
\Models\User::register('users');

Now, you can directly get the User instance from a query. Example:

$user = \Models\User::query_row("SELECT id, name FROM users WHERE id = 1 AND active = 1");

// You can also do this
// 1 is the id (primary key)
$user = \Models\User::instance(1);

// you can call the get_name() method now
if ($user) {
    $name = $user->getName();
    echo 'His name is '.$name;
}

Queries

To query multiple rows of data, you can provide your own SQL to the query method of the Model. For example:

$users = \Models\User::select("SELECT * FROM users WHERE active = 1");
foreach ($users as $user) {
    $name = $user->getName();
    echo 'User #'.$user->id.' '.$name.'<br>';
}
Pro tip!
You can pass the $data to the \Bootstrap\Components\Table class to create a table. See PHP Components / Table page for more information.

Credits

SmartAdmin uses lodev09/php-models package to connect and CRUD your database. Created by @lodev09.