Models

Models in Yabasi represent the data structure and business logic of your application. They interact with the database, handle relationships between different entities, and provide an elegant way to work with your data.

Defining Models

To create a model, extend the Yabasi\Database\Model class:

User.php
namespace Yabasi\Models;

use Yabasi\Database\Model;

class User extends Model
{
    protected static string $table = 'users';
    protected array $fillable = ['name', 'email', 'password'];
}

Creating and Saving Models

You can create and save model instances like this:

UserController.php
$user = new User();
$user->setName("John Doe");
$user->setEmail("john@example.com");
$user->setPassword("secret");
$result = $user->save();

echo "User save result: " . ($result ? "true" : "false");

Retrieving Models

Yabasi provides various methods to retrieve models:

UserController.php
// Find by ID
$user = User::find(1);

// Find by email
$user = User::query()->where('email', 'john@example.com')->first();

// Get all users
$allUsers = User::all();

// Count users
$userCount = User::count();

Updating Models

You can update model attributes and save changes:

UserController.php
$user = User::find(1);
$user->setName("John Updated");
$updateResult = $user->save();
echo "User update result: " . ($updateResult ? "true" : "false");

Deleting Models

To delete a model:

UserController.php
$user = User::find(1);
$deleteResult = $user->delete();

Query Building

Yabasi provides a fluent query builder:

UserController.php
$recentUsers = User::query()
    ->where('name', 'LIKE', 'John%')
    ->orderBy('created_at', 'DESC')
    ->limit(5)
    ->get();

Relationships

Yabasi supports various types of relationships between models, allowing you to easily work with related data:

One-to-Many Relationship

User.php
class User extends Model
{
    public function posts() {
        return $this->hasMany(Post::class);
    }
}

Belongs-To Relationship

Post.php
class Post extends Model
{
    public function author() {
        return $this->belongsTo(User::class);
    }
}

Using Relationships

You can easily access related models:

UserController.php
$user = User::find(1);
$posts = $user->posts;
foreach ($posts as $post) {
    echo $post->title . " by " . $post->author->name . "\n";
}

$post = Post::find(1);
echo "Author: " . $post->author->name;

Eager Loading

To avoid the N+1 query problem, you can use eager loading:

UserController.php
$users = User::with('posts')->get();
foreach ($users as $user) {
    echo $user->name . " has " . $user->posts->count() . " posts.\n";
}

Mass Assignment

Yabasi allows you to set multiple attributes at once using the fill() method:

UserController.php
$user = new User();
$user->fill([
    'name' => 'Jane Doe',
    'email' => 'jane@example.com',
    'password' => 'secret'
]);

$result = $user->save();
echo "User save result: " . ($result ? "true" : "false");

Accessors and Mutators

You can define accessors and mutators to format attribute values when retrieving or setting them:

User.php
class User extends Model
{
    public function getName(): string
    {
        return ucfirst($this->attributes['name']);
    }

    public function setPassword($value): void
    {
        $this->attributes['password'] = password_hash($value, PASSWORD_DEFAULT);
    }
}

These features of Yabasi's ORM allow you to work with your database in an object-oriented manner, making your code more readable and maintainable. The relationships feature, in particular, helps you to efficiently work with related data across multiple tables.

Find the documentation helpful?

Show your support by starring our project on GitHub

Star on GitHub