-
Notifications
You must be signed in to change notification settings - Fork 7.6k
SuperModel
Category:Libraries::SuperModel | Category:Libraries::Community | Category:Libraries::External
The SuperModel Library is an extension to models to automate most of the mundane form-generation and validation tasks. Think of it as scaffolding on steroids. The model definition contains a list of fields and configuration attributes (such as label, type, and rules). There is a form generation function that creates an XHTML form using divs, which allows for various layout options using css (Supermodel::Html Output). SuperModel also takes care of loading, validation, saving (updating or inserting), and joins.
[em]Please note this library is a work-in-progress. I am currently making many changes, including API changes that will break applications. As I write this (May 30/2006) I am working on implementing one<>many and many<>many joins.[/em]
It's impossible to write something like this, but stay as flexible like CodeIgniter is. Unfortunately, this library forces you into doing some things a certain way. I've tried to be as flexible as possible, but at the same time, there has to be a line drawn between being flexible, and being completely bloated. That's why this is an external 3rd party library - you're free to implement models the way you want, or use some other similar 3rd party library that does something similar.
I use ADODB as a database backend instead of CI's database library, because it is a bit more mature and full featured. If you want to use CI's database library, you'll have to rewrite some of this, however there is no reason that the two cannot co-exist.
<?php
class user_model extends SuperModel {
var $fields = array(
'name' => array(
'label' => 'User Name',
'type' => 'text',
'rules' => 'required|unique|trim|min_length[3]|max_length[15]',
),
'display_name' => array(
'label' => 'Display Name',
'type' => 'text',
'rules' => 'required|unique|trim|min_length[3]|max_length[15]',
),
'password' => array(
'label' => 'Password',
'type' => 'password',
'rules' => 'required|min_length[5]|max_length[15]',
),
'type' => array(
'label' => 'User Type',
'type' => 'dropdown',
'options' => array('guest','normal','moderator','admin'),
'default' => 'normal',
),
'submit' => array(
'type' => 'submit',
'value' => 'Save',
),
);
var $table = 'users';
var $primary = 'user_id';
var $lists = array(
'fulllist' = array('username');
);
}
?>
For more information on fields, see SuperModel::Fields. For types and their corresponding HTML controls, see SuperModel::Controls.
$table defines the database table this model represents.
$primary is the primary ID field of this database. It should be unique. Normally it will have an auto increment or sequence attached to it, though it doesn't necessarily have to be numeric, and it can also be defined for user input in the $fields array.
$lists is an array with lists for use by GetList(). See SuperModel::API::GetList for more information.