Generate Models

Apex provides a CLI command that will go through the specified database table including all columns and foreign key constraints, then proceed to generate the model class itself plus any necessary model classes for foreign key constraints.

Setup Database

If you already have a database table or several created that you wish to create models for, you may skip to the next section. Otherwise, ensure you already have a package created and if you don't have one, quickly create one with the command:

apex package create demo

Open the file at /etc/demo/install.sql, and enter the following contents to create some demo tables:

CREATE TABLE demo_categories (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    is_active BOOLEAN NOT NULL DEFAULT true,
    slug VARCHAR(100) NOT NULL UNIQUE,
    name VARCHAR(100) NOT NULL
);

CREATE TABLE demo_products (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    category_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    is_active BOOLEAN NOT NULL DEFAULT true,
    in_stock INT NOT NULL DEFAULT 0,
    price DECIMAL(8,2) NOT NULL DEFAULT 0,
    description TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES demo_categories (id) ON DELETE CASCADE
);

CREATE TABLE demo_product_reviews (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    product_id INT NOT NULL,
    full_name VARCHAR(255) NOT NULL,
    review TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (product_id) REFERENCES demo_products (id) ON DELETE CASCADE
);

Execute the SQL against your database either manually, or with the command:

apex migration install demo

Generate Model Classes

Generate the model classes for all three above database tables with the command:

apex opus model Demo/Models/Product --dbtable demo_products --magic

The above command will ask you to confirm both, creation of the parent directory and whether or not you wish to generate a model for the foreign key relationships found. Select yes for both, and you will notice three new files located within the ~/src/Demo/Models/ directory. If you have different database tables to create models for, then change the command above accordingly.

Some notes about the above command:

  • First and only argument is location where yyou would like the new model class saved, relative to the /src/ directory. The ".php" extension is optional.
  • The --dbtable flag is required, and is the name of the database table to create the model for.
  • The --magic flag is optional, and if not present the model classes will be generated with the standard getter / setter methods. Otherwise if present, you can access the properties directly at $obj->property_name.

If desired, you may view the documentation page for this command by visiting the, opus model CLI Command page.

Anatomy of Models

Open the file now located at /src/Demo/Models/Product.php, and you will notice they are rather small PHP classes. A few notes regarding the model classes:

  • The $dbtable property must be present, and set to the name of the database table the model represents.
  • Constructor property promotion is used with one property for each column within the database table. Column type, default values, and nullables have been taken into account when generating the properties.
  • The foreign keys were taken into account, with the necessary methods generated in each model class to retrieve the parent / child objects.
  • With the --magic flag set, there are no getter / setter methods for each property, and instead you may get / set all properties directly at $obj->property_name.
  • If you prefer standard getter / setter methods for each property, simply leave out the --magic flag when generating the model.