Skip to content
Jared King edited this page Jun 19, 2013 · 3 revisions

At the heart of an MVC architecture are models. Models represent some reproducible entity, like a user, a task, or a bank account. Typically, models will be backed by persistent storage. Infuse Framework includes boilerplate code for managing models, such as, creating, editing, and deleting. All of the interactions with the database have been abstracted out, although you are free to override any of the model functions.

Model Definition

Example for a user model. This file would go in modules/users/models/User.php.

<?php

namespace infuse\models;

class User extends \infuse\Model
{
    public static $properties = array(
        'id' => array(
            'type' => 'id'
         ),
        'username' => array(
            'type' => 'text'
            'length' => 25
         ),
         'password' => array(
             'type' => 'password'
         ),
         'address' => array(
              'type' => 'longtext',
              'length' => 200
          )
    );
}

In order to take advantage of the boilerplate model code, you must define the properties of the model. The name of the property cannot contain spaces. It is advised to use lowercase names with spaces delimited by an underscore (_).

Available options for a property

type

The type of the property.

Accepted Types:

  • id
  • text
  • longtext
  • number
  • boolean
  • enum
  • password
  • date
  • hidden
  • custom
  • html

Type: String

Required

default

The default value to be used when creating new models.

Type: String

Optional

number

The type of number when the property type is number.

Type: String

Default: int

Required if specifying number type

enum

A key-value map of acceptable values for the enum type.

Type: Array

Required if specifying enum type

enumType

Type of the database column for the enum.

Type: String

Default: varchar

Required if specifying enum type

length

Overrides the default maximum length of the column values in the database. Use this when a different value is needed besides the one specified.

Type: Integer|String

Default: Chosen according to type

Optional

null

Specifies whether the column is allowed to have null values.

Type: Boolean

Default: false

Optional

filter

An HTML string that will have values from the model injected. Only used in the admin panel.

Type: String

Example: <a href="/users/profile/{uid}">{username}</a>

Optional

required

Specifies whether the field is required when creating new models.

Type: Boolean

Default: false

Optional

validation

Function reference to validate the input of the field (i.e. user creation, editing a user), returns true if valid. The function should look like function validate_email( &$property_value, $parameters ) The validation function is allowed to modify the property value.

Type: Array

Optional

validation_params

An array of extra parameters to pass to the validation function. Comes through the second argument in an array.

Type: Array

Default: null

Optional

nosort

Prevents the column from being sortable in the admin panel.

Type: Boolean

Default: false

Optional

nowrap

Prevents the column from wrapping in the admin panel.

Type: Boolean

Default: false

Optional

truncate

Prevents the column from truncating values in the admin panel.

Type: Boolean

Default: true

Optional

title

Title of the property that shows up in admin panel.

Type: String

Default: Derived from property name

Optional

Clone this wiki locally