Models



The basic rationale for using model filesin Models / directory isbased on ensuring that database queries are written once and repeatedly. Are they just questions? No, it can be written in different codes that are considered to be included externally.

 

# Section Headings


# Creating a Model File

# Including Model Controller

# Using Namespaces in Model Files

# Export Model Controller Namespace

# Internal Model Design

# Namespace Invisibility

# Grand Model Extension

# Interest Model Extension

 

# Creating a Model File


You can create a sample model file as follows.

File: Projects / Frontend / Models / UsersModel.php
<?php 
class UsersModel extends Model
{
    public static function rowId(Int $id)
    {
         return DB::whereId($id)->users()->row();
    }
}

Including Model Controller

All you have to do is call your model in the controller. If your model class does not contain a namespace and the model name and controller name are the same, you need to give your model file an alias name.

File: Projects / Frontend / Controllers / Users.php
<?php namespace Project\Controllers;

use UsersModel;

class Users extends Controller
{
    public function main()
    {
         output( UsersModel::rowId(5) );
    }
}

 

# Using Namespaces in Model Files


When the model classes have the same name as the controllers, you need to provide the namespace of the Model class to avoid conflicts between these two classes.

File: Projects / Frontend / Models / Users.php
<?php namespace Model;

use Model, DB;

class Users extends Model
{
    public static function rowId(Int $id)
    {
         return DB::whereId($id)->users()->row();
    }
}

Now to use this model file;

File: Projects / Frontend / Controllers / Users.php
<?php namespace Project\Controllers;

use Model\Users;

class Users extends Controller
{
    public function main()
    {
         output( Model\Users::rowId(5) );
    }
}

Export Model Controller Namespace

If you do not want to use your model files when using them in controllers. Give your model the same namespace as the controller Project \ Controllers . When you do this, you do not have to define the model with the use in the invoked controller  .

File: Projects / Frontend / Models / UsersModel.php
<?php namespace Project\Controllers;

use Model, DB;

class UsersModel extends Model
{
    public static function rowId(Int $id)
    {
         return DB::whereId($id)->users()->row();
    }
}

Now to use this model file;

File: Projects / Frontend / Controllers / Users.php
<?php namespace Project\Controllers;

class Users extends Controller
{
    public function main()
    {
         output( UsersModel::rowId(5) );
    }
}

 

# Internal Model Design


Define your dynamic model classes by using the Internal prefix of the class name to use in the Model :: method () form . If the class method is not static in normal conditions, it will not be accessible in the form above. However, because static definitions restrict some uses, class designs are implemented in dynamic form. Despite these dynamic definitions, it is possible to access static methods statically via this prefix.

<?php
class InternalUsersModel extends Model
{
    public function rowId(Int $id)
    {
        return DB::whereId($id)->users()->row();
    }
}

After the above definition, you can use the model file as follows.

output( UsersModel::rowId(1) );

Namespace Invisibility

Even with namespaces defined by internal prefixes namespaceloses functionality. Because the internal prefix used is not the class in which it is used, but its static appearance.

<?php namespace Model;

use Model, DB;

class InternalUsersModel extends Model
{
    public function rowId(Int $id)
    {
        return DB::whereId($id)->users()->row();
    }
}

After the above definition, you can use the model file as follows.

File: Projects / Frontend / Controllers / Users.php
<?php namespace Project\Controllers;

use UsersModel;

class Users extends Controller
{
    public function main()
    {
         output( UsersModel::rowId(5) );
    }
}

As you can see, although our model contains namespace, we have not defined it with use in the controller. Because the model we use is the InternalUsersModel class, not its static view , the UsersModel class.

 

# Grand Model Extension


Grand Model Extension is designed to make database methods more useful and practical. Click for more . As an example on the line , we do not need to define the rowId () above for this extension.

<?php
class InternalUsersGrand extends GrandModel
{
    
}

After the above definition, you can use the model file as follows.

output( UsersGrand::rowId(1) );

As you can see, though it is hollow, it does use the above.

There are many useful and simple methods that GrandModel expansion can bring. These methods can be found at the link given above.

 

# Interest Model Extension


It is a mode designed to merge multiple tables. Establish relationships between specific tables. With this relationship, common data collection, updating, deletion, etc. are common to the related tables. It is a modeled version of a nested join process. Click for more .