Models

In Lightweight MVC, a Model binds a post type.

Store your models at theme/Models folder on themes and plugin/Models for plugins.

Create

When creating a model, the minimum things required are:

  • Specify a namespace.
  • Add use to include the Model class.
  • Add use to include the FindTrait.
  • Extend from Model.
  • Use FindTrait.

Example:

<?php

namespace MyApp\Models;

use Amostajo\LightweightMVC\Model;
use Amostajo\LightweightMVC\Traits\FindTrait;

class Post extends Model
{
    use FindTrait;
}

The example above will create a model with the default post type post.

Use

Add the use statement at the beginning of your php files to specify which models you’ll use in your code.

Example:

use MyApp\Models\Post;

Find

Find a record by calling to the static find() function. Pass by a Post ID as parameter.

Example:

$post = Post::find( $id );

Get the children posts of a parent by calling to the from() method.

Example:

$posts = Post::from( $id );

Save

Saving the information of a model is a piece of cake. Simply call the save() function after changing its attributes. This will insert or update the record.

Example:

$post->post_title = 'My awesome post';

$post->save();

Delete

Call the delete() function. This will move the post to trash.

Example:

$post->delete();

Add the $forceDelete property inside your model class to force deletions (no trash).

Example:

class Post extends Model
{
    use FindTrait;

    protected $forceDelete = true;
}

Access meta data

Access post meta by using the meta property. Example:

$meta = $post->meta; // ARRAY

Casting

  • To array:
    $value = $post->to_array();
  • To json:
    $value = $post->to_json();
  • To WP_Post:
    $value = $post->to_post();

Aliases

A powerful feature that will let you add aliases to post attributes, meta values and custom functions. Add the $aliases property inside your model class to use it.

As attributes

Definition example:

class Post extends Model
{
    use FindTrait;

    protected $aliases = [
        'title' => 'post_title',
        'content' => 'post_content',
    ];
}

Usage example:

$post->title = 'Alias usage title';
$post->save();

As meta values

Add a meta key as alias with meta_ as prefix.

Example:

class Post extends Model
{
    use FindTrait;

    protected $aliases = [
        'title'     => 'post_title',
        'content'   => 'post_content',
        'price'     => 'meta_price', // Meta key "price"
    ];
}

Usage example:

echo $post->price; // Echos price value

$post->price = 9.99;
$post->save();

As functions

Add a function as alias with func_ as prefix, these are READ-ONLY model properties.

Example:

class Post extends Model
{
    use FindTrait;

    protected $aliases = [
        'title'     => 'post_title',
        'content'   => 'post_content',
        'price'     => 'meta_price', // Meta key "price"
        'is_free'   => 'func_is_free',
    ];
    protected function is_free()
    {
        return $this->price <= 0;
    }
}

Usage example:

echo $post->is_free; // Echos true or false

Hidden properties

Sometimes you might need to hide properties (attributes or aliases) from appearing on json or array. Add the $hidden property inside your model class to use hide these when casting the model.

class Post extends Model
{
    use FindTrait;

    protected $hidden = [
        'post_date', 'password'
    ];
}

Types

Add the $type property inside your model class to change its post type.

Example:

class Book extends Model
{
    use FindTrait;

    protected $type = 'book';
}

Status

By default any new record created with a model has the status of draft. Add the $status property inside your model class to change its post status on insert.

Example:

class Book extends Model
{
    use FindTrait;

    protected $status = 'publish';
}