Base model for the laravel PHP Framework for use with the Squi UI generation bundle.
Use the Laravel artisan CLI tool to download the bundle:
php artisan bundle:install modest
Add an entry into application/bundles.php
:
return array(
// ... existing bundles
'squi' => array('auto' => true),
'modest' => array('auto' => true),
);
Modest expands on the usefulness of Models as a utility for working with data structures by learning more about the entities it is working with. This means there is a little more work up front, but that effort is duly rewarded during application development.
At the most basic level, Modest needs a list of the fields in the database table your model represents. Let's take a simple User model that lists the user's name, their gender and the group they belong to:
public static $_properties = array(
'id',
'first_name',
'last_name',
'gender',
'group_id',
);
This doesn't give the model much to work with, but at least some things like the properties_display()
method can function:
$user = User::find(123);
print_r( $user->properties_display() );
Output:
Array
(
[id] => Array
(
[label] => Id
[value] => 123
)
[first_name] => Array
(
[label] => First Name
[value] => John
)
[last_name] => Array
(
[label] => Last Name
[value] => Brooks
)
[gender] => Array
(
[label] => Gender
[value] => male
)
[group_id] => Array
(
[label] => Group Id
[value] => 3
)
)
Notice that the label
of each property has been roughly formatted to be a human-readable label based on the property name. The properties_display()
method might be used to display the user's information on the page along with labels, without the need for manually adding each field on the controller side.
This becomes more useful when you tell Modest more about your fields:
$_properties = array(
'id' => array(
'display' => false,
),
'first_name',
'last_name',
'gender' => array(
'form' => array(
'type' => 'radio',
'options' => array(
'female' => 'Female',
'male' => 'Male',
'other' => 'Something Else',
),
),
),
'group_id' => array(
'label' => 'Group',
'form' => array(
'type' => 'select'
'options_table' => array(
'table' => 'groups',
'label' => 'name',
'value' => 'id',
),
),
),
);
That's a lot to absorb, but this gives the model plenty of information to go on. For the id
field, we're telling Modest that it should not be displayed. For the gender
field, we're letting it know how information would be entered into the field. It's a radio field with three options that we explicitly define. Finally, group_id
refers to a record in the groups
table, with the label being in the name
column of that table. We're also explicitly defining a label of "Group" for that property.
Now, a call to $user->properties_display()
(after some formatting) would have show this:
First Name: John
Last Name: Brooks
Gender: Male
Group: Editors
Beautiful, right? You only need to define those details once, and take advantage of the benefits from anywhere in your app. This encourages the design pattern of "Fat models, slim controllers".
These formatted values are actually a side effect of Modest knowing about how those properties would look as form fields. The really neat part is that it can now generate a complete form, thanks to the Squi bundle.