MVC For MVC projects (see the flavor table in Overview).
In an MVC project, make:scaffold writes CRUD pages for one table of the database: a model, a form, a controller and three views. In a project from crest new --flavor=mvc with a database, and with a table users:
crest make:scaffold usersCreated src/Models/Base/UsersBase.php
No relations for users: the table has no foreign keys, and no foreign keys point to it. Relations come only from foreign keys.
Created src/Models/Users.php
Created src/Forms/Base/UsersFormBase.php
Created src/Forms/UsersForm.php
Created src/Controllers/UsersController.php
Created themes/default/users/index.phtml
Created themes/default/users/new.phtml
Created themes/default/users/edit.phtmlA project from crest new --flavor=mvc already has CrestControllerBase. Thus Crest prints no line for it while the base is the same as its stub. Crest then prints what the pages need: the default namespace of the dispatcher, the URL of the list, and the base URI. It also tells you that the pages have no login and no CSRF check.
| File | Owner | A second run |
|---|---|---|
src/Models/Base/UsersBase.php |
Crest | writes it again |
src/Models/Users.php |
you | keeps it, also with --force |
src/Forms/Base/UsersFormBase.php |
Crest | writes it again |
src/Forms/UsersForm.php |
you | keeps it, unless --force |
src/Controllers/Base/CrestControllerBase.php |
Crest | writes it again |
src/Controllers/UsersController.php |
you | keeps it, unless --force |
themes/default/users/index.phtml |
you | keeps it, unless --force |
themes/default/users/new.phtml |
you | keeps it, unless --force |
themes/default/users/edit.phtml |
you | keeps it, unless --force |
Crest writes a base again only if the file is not the same as the base that Crest makes: from the table for UsersBase and UsersFormBase, and from the stub controller-base for CrestControllerBase. Thus when the table gets a new column, run make:scaffold again. Crest writes the two bases again, and the list and the new and edit pages show the column, because the views loop over the form. The rules of Generated bases apply to the two bases. make:model --force replaces the model.
With models.schema, the pages read the table of that schema (see The schema).
In a Volt project ('templateEngine' => 'volt', see Volt), the views are index.volt, new.volt and edit.volt. The Volt url_encode filter encodes a text key in the links, so the controller decodes it with urldecode(). With phtml, the views use rawurlencode() and the controller rawurldecode().
The names of the pages
All the names come from the class of the model. Thus one line of models.classes changes all of them (see Names):
| Name | users |
user_profiles |
users with 'users' => 'User' |
|---|---|---|---|
| Model | Users |
UserProfiles |
User |
| Form | UsersForm |
UserProfilesForm |
UserForm |
| Form base | UsersFormBase |
UserProfilesFormBase |
UserFormBase |
| Controller | UsersController |
UserProfilesController |
UserController |
| URL and views folder | users |
user-profiles |
user |
The name of a field is the property of its column, and its label comes from the column: first_name gives the label First name.
The pages
The list and the new and edit pages use the form that make:scaffold writes (see The form).
| URL | Method | Result |
|---|---|---|
/users |
any | the list, with 10 rows on each page, and the search form |
/users/search |
any | the list of the rows that match the query |
/users/new |
any | an empty form |
/users/create |
POST | adds a row, and goes to the list |
/users/edit/<id> |
any | the form of the row |
/users/save/<id> |
POST | changes the row, and goes to the list |
/users/delete/<id> |
POST | deletes the row, and goes to the list |
- A request to
create,saveordeletethat is not a POST goes to the list, and changes nothing. - When the model refuses a value, the form shows again, with the messages of the model and the values that the user typed. When the model refuses a delete, the list shows its messages.
createdoes not change a row that exists: a primary key that is in the table gives the message of the model, and the form shows again.- A row that does not exist gives
Users was not found, and the list.
To write the pages into a module, add --module (see Generating modules):
crest make:scaffold users --module=adminThe model, the form and the controller go into the Models, Forms and Controllers folders of the module, in its namespace, and the views go into views/users/. The pages are at /admin/users, after you add the registration and the routes that make:module prints. The module must exist.
What stops the command
Crest does these checks before it writes a file. Thus a run that stops changes nothing. It stops when:
- the project is not an MVC project;
- the project has no views (
'views' => false); templateEngineis notphtmlorvolt;--modulenames a module that does not exist;- the table name gives no class name, and
models.classesgives it no class; - the dispatcher cannot find a controller of that class from a URL (see Generating controllers);
- no psr-4 entry covers the
form,controllerormodelpath; - the table does not exist;
- the table has no primary key, or its primary key has more than one column;
make:modelstops on the table (see Whenmake:modelstops);- a property has the name of an accessor of
Phalcon\Mvc\Model(see below); - the
editform has no fields, for example when the table has only its primary key.
A column with the name of a Model accessor. Phalcon\Mvc\Model has methods such as setSource(), getSchema() and getMessages(). A form calls set<Name>() and get<Name>() when they exist. Thus a column source calls Model::setSource(), and each save fails. Crest stops, and gives the command that gives the column another property name:
crest: column 'source' gives the property 'source', and Phalcon\Mvc\Model has a method with that name; give it another name with make:model sources --property=source=<name>Run that make:model command, then run make:scaffold again. With models.getSet, the check of make:model for the getters and setters comes first, and Crest stops with the message of Getters and setters.
The form
The form has one field for each column, in the order of the table:
| Column | Field | Filter | Other |
|---|---|---|---|
| The identity column | none | ||
| A primary key that is not identity | as the rows below | as the rows below | on the new form only |
Name is email (any case) |
Email |
email |
|
BIGINTEGER, INTEGER, MEDIUMINTEGER, SMALLINTEGER, TINYINTEGER |
Numeric |
int |
|
DOUBLE, FLOAT |
Numeric |
float |
step="any" |
DECIMAL |
Numeric |
trim |
step="any" |
BOOLEAN |
Select |
bool |
the options No and Yes, and an empty option |
DATE |
Date |
trim |
|
TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT, JSON, JSONB |
TextArea |
trim |
|
BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB, BINARY, VARBINARY, BYTEA |
none | Crest prints Skipped <column>: a binary column has no field in the form |
|
| Each other type | Text |
trim |
The column type is the type that Phalcon reads from the database (see Generating models). An input that is empty or has only whitespace is null, and the model decides if the column can be null: a NOT NULL column gives the message <property> is required.
The pages also work with models.getSet: the form and the views go through the getters and the setters (see Getters and setters).
Search
The search form has the fields of the new form, and uses the rules of Phalcon\Mvc\Model\Criteria::fromInput(): LIKE for a VARCHAR column, and = for each other type. An empty field is not in the search. On PostgreSQL, partial text for a DATE, a TIMESTAMP or a UUID column gives an error page.
The base URI
The links and the redirects use the url service. With no base URI, Phalcon makes one from PHP_SELF, and the links of a page such as /users/edit/5 are not correct. A project from crest new sets / in UrlProvider. For an application in a subfolder, change that line, for example to /shop/. Another project must set the base URI:
$url->setBaseUri('/');Limits
- The pages have no login and no CSRF check. Add them before you deploy the pages.
- There is no success message after a redirect, because a message that stays after a redirect needs a session. When your project has a
sessionservice, call$this->flashSession->success('Saved')before the redirect, and add<?php $this->flashSession->output() ?>to the view. - A foreign key column gets the field of its type, for example a
Numericfield, not a list of the related rows. - A
DELETEthat a foreign key refuses gives an error page. - A column with the name
pageis also the parameter of the page in the URL.