Models

Models are where the framework host files that directly interacts with the database. Currently MightyPHP only works with MySQL and MariaDB databases.

Overview

A typical Model in MightyPHP would look like this:

namespace Application\Models;
use MightyCore\Database\Model;
class ExampleModel extends Model
{
}

Generating a Model

To generate a model automatically, you can use the below command:

php mighty make:model ExampleModel

Table Name

By default, the table name is defined by the model's class name. For a model with the class name of ExampleModel would result in the framework pointing to the table, example in the database.

Notice how if your model class name contains the Model suffix, it is ignored and not taken into account for table name matching.

Pointing to a different table

Simply declare it in the model class as such:

namespace Application\Models;
use MightyCore\Database\Model;
class ExampleModel extends Model
{
protected $table = 'another_table';
}

Connection

MightyPHP supports multiple database connection. You can configure the connection through the .env.

DB_DEFAULT_HOST=127.0.0.1
DB_DEFAULT_PORT=3306
DB_DEFAULT_USERNAME=root
DB_DEFAULT_PASSWORD=secret
DB_DEFAULT_DATABASE=example

Notice that the syntax is DB_connection_XXX. Where the connection is as user defined followed by the attribute you wish to add. For example, if you wish to add another connection which will be called foo.

DB_FOO_HOST=127.0.0.1
DB_FOO_PORT=3306
DB_FOO_USERNAME=root
DB_FOO_PASSWORD=secret
DB_FOO_DATABASE=foo

MightyPHP's default connection points to default, therefore it is crucial for your default database to be declared.

By default, MightyPHP points the connection to default. If you have another connection that you wish to declare for a certain model, you may declare it as such:

namespace Application\Models;
use MightyCore\Database\Model;
class ExampleModel extends Model
{
protected $connection = 'foo';
}