Elefant provides a simple CRUD generator as a command line tool to help developers save time by avoiding repetitive boilerplate code.
To create a new Elefant app with auto-generated CRUD handlers, run the following command from the root of the site:
./elefant crud-app contact id name email phone added
This will generate the following:
apps/contacts
Contacts
entry under Elefant's Tools menuapps/contacts/conf/install_*.sql
contactsContact
used to access the database tableapps/contacts/forms
Starting with the example command above, the following steps will help you customize a new CRUD app.
Edit the appropriate table schema file under apps/contacts/conf
, changing fields to different types, etc. For example, you may want to change added
to be a datetime
field.
When you're ready, import the table schema via the following command:
./elefant import-db apps/contacts/conf/install_sqlite.sql
Note: Be sure to specify the correct schema file for your database.
Next, edit the form validation rules and add ones specific to your fields. These are found under apps/contacts/forms
. By default, all fields are marked as required.
In our case, we can add email = 1
to the [email]
section, so that email addresses are validated correctly.
You should also edit the add.html
and edit.html
view templates under apps/contacts/views
and adjust the validation notices, and change any input types (by default they're all single-line text inputs).
In the above example, we'll want to remove the added
field from the views altogether, and also make two changes to the handlers:
Edit apps/contacts/handlers/add.php
and change this line:
'added' => $_POST['added']
To this:
'added' => gmdate ('Y-m-d H:i:s')
Edit apps/contacts/handlers/edit.php
and remove the following line:
$contact->added = $_POST['added'];
This will ensure the current date/time is set when a new contact is added, and the edit form will not affect this value on subsequent edits.
Edit the file apps/contacts/views/admin.html
and customize the columns that are displayed. For example, let's add the added
field to the list of fields shown, and make the email field a mailto:
link:
{! admin/util/dates !}
<p><a href=""/contacts/add"">{""Add Contact""}</a></p>
<p>
<table width=""100%"">
<tr>
<th width=""21%"">{"" Name ""}</th>
<th width=""21%"">{"" Email ""}</th>
<th width=""21%"">{"" Phone ""}</th>
<th width=""21%"">{"" Added ""}</th>
<th width=""16%""> </th>
</tr>
{% foreach items %}
<tr>
<td>{{ loop_value->name }}</td>
<td><a href=""mailto:{{ loop_value->email }}"">{{ loop_value->email }}</a></td>
<td>{{ loop_value->phone }}</td>
<td>{{ loop_value->added|I18n::date_time }}</td>
<td>
<a href=""/contacts/edit?id={{ loop_value->id }}"">{""Edit""}</a> |
<a href=""/contacts/delete?id={{ loop_value->id }}""
onclick=""return confirm ('{""Are you sure you want to delete this contact?""}')"">{""Delete""}</a>
</td>
</tr>
{% end %}
</table>
</p>
{! navigation/pager?style=text&url=[url]&total=[total]&count=[count]&limit=[limit] !}
What we've changed here are the following:
{! admin/util/dates !}
line to include the localized date support.added
field to the table headers and adjusted the header widths.email
field a mailto:
link.added
field to the loop, filtering it with I18n::date_time
to format it and localize it to the current user's timezone.From here, you can edit your apps/contacts/handlers/index.php
handler to provide a public-facing component to your app, or use the contactsContact
model to integrate your contacts into other areas of your web application.
As you can see, Elefant's CRUD generation capabilities are a simple but powerful time saver for developers of all skill levels.