AppModel

Creating a model file

To generate a Model in which you can add business logic but cannot CRUD entities to Wordpress’ database, you should use the automated generator provided by Strata. It will validate your object’s name and ensure it is defined following the intended conventions.

Look at automated custom post type models for information on how to create models that map to database entries.

Using the command line, run the generate command from your project’s base directory. In this example, we will generate a model named Artist :

$ ./strata generate model artist

It will generate a couple of files for you, including the actual model file and test suites for the generated class.

Scaffolding model Artist
  ├── [ OK ] src/Model/Artist.php
  ├── [ OK ] src/Model/Entity/ArtistEntity.php
  └── [ OK ] test/Model/ArtistTest.php
  ├── [ OK ] test/Model/Entity/ArtistEntityTest.php

Building a data supplier

The idea behind AppModels not associated to a Wordpress custom post type is to have a single unique place where you can store information and behavior in a meaningful way. For instance, the Artist model can supply a list of genre’s throughout the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace App\Model;

class Artist extends AppModel
{
    public static function listGenres()
    {
        return array(
            "Classical",
            "Pop",
            "Rock",
        );
    }
}

A Model is the perfect type of object unto which add API access to a remote services.