Defining models and data


Defining models

You can define which fields a model has by defining the data types in the __model magic method.

Here’s the syntax for a Book model as an example:

static function __model(){
	/////////////////////////////////////////
	// begin custom fields definition:
		$f = (object) array();
		$f->title = zajDb::name(100);
		$f->author = zajDb::manytoone('Author');	// A connection to the Author object
		$f->synopsis = zajDb::textarea();               // A review of the book
		$f->likecount = zajDb::integer();		// The number of times users have liked this book
		$f->locale = zajDb::locale()->default('en_US');	// Locale (language of book)
		$f->genre = zajDb::select(array('Fiction','Nonfiction')); // Genre select

	// do not modify the line below!
		$f = parent::__model(__CLASS__, $f); return $f;
}

The syntax is fairly simple. You define the name of the field and you set it to the type definition. Options directly relating to the type definition are passed as parameters.

Once you create your model, you can then go to the Outlast Framework update menu at http://example.com/update/ and run a database update. This will create all the fields for you in the database and/or list any modifications you may need to perform manually (such as removing fields no longer in use).

Built-in data types

Outlast Framework comes with a healthy dose of data types. Data types provide definitions to how OFW should store and retrieve data from the database. In addition, they also come with field editors which make it easy to build a user interface for editing the data.

Many data types are simple, like text, which is merely a string stored in the database and edited with a standard HTML input field. Others are more complex, like photos, which connects to several Photo objects in the database and provides an advanced plupload-based photo uploading interface.

Creating, accessing, and editing manytoone, manytomany, onetomany, and onetoone connections is also built-in. You do not have to create connection tables and most of the time you do not have to write JOIN queries.

There are many more data types available. For a full list of types and options see Data Types.

You can also define your own custom data types. See docs below.

Built-in inputs / editors

Built-in inputs are a really powerful feature that allows you to generate anything from simple text inputs to complex data items like photo uploaders and user-friendly relationship search boxes.

In your templates you can simply use the following code to generate the data type’s editor:

{% input mymodel.myfield myobject.data.myfield %}

The first parameter is the model and field, the second is the current value that you want to set it to.

You can also override the editor HTML template:

{% input mymodel.myfield myobject.data.myfield 'myown/editor/template.html' %}

To create your own editor, follow the documentation guide.

You can also override the default editors for your project. By default, field editor HTML templates are located in the system folder at /system/plugins/_jquery/view/field/fieldname.field.html. If you want to modify any of the default editors for your project, you can simply create a copy in your local app’s view folder as /app/view/field/fieldname.field.html – because of plugin hierarchy, Outlast Framework will use your local copy instead of the system version. You can read more about customizing data types and fields here.

Custom data types and fields

All of Outlast Framework’s built-in data types are created as class extensions of the zajField class. You can use the zajField class to build your own data types or you can override built-in data types, data field editors, etc. in any way, shape, or form you wish to. You can read more about customizing data types and fields here.

Alias fields

Outlast Framework supports alias fields or field pointers. Alias fields reference the same exact data field with two different names – this can be useful for example when you need backwards compatibility with older code or you simply want two names for the same field for convenience.

To declare alias fields, you will need to use the same syntax as with standard fields. Alias fields also need to be the same type as the field they reference.

$f->my_text_field = zajDb::text();
$f->my_other_field = zajDb::text()->virtual('my_text_field');

Both fields will be stored as my_text_field in the database, but it can be referenced from OFW as either my_text_field or my_other_field.

Outlast Web & Mobile Development (c) 2018 | Privacy Policy |