Quick links: Content - sections - sub sections
EN

Trace: crud

Wiki: Sitemap - Recent Changes - Back link

We saw how to create a simple form. However, we didn't have a full interface to administrate the data: verification of the content, display of errors, list of the news and links to modify, delete…

Jelix facilitate the development of such features with a powerful form system and a controller to do CRUD (Create/Read/Update/Delete).

Creating a form with jForms

The first thing to do is to create a form using jForms. It is an XML file in which you declare all controls of the form, and their labels, their constraints etc.

It exists a command in jelix-scripts to create quickly a jForms form from a dao file. We will use of course the dao we created in the previous chapter. Go to the jelix-scripts directory and execute this command:

php dev.php module:create-form news newsform news

The createform command takes three arguments :

  1. the name of the module,
  2. the name of the futur form,
  3. the name of the dao.

Then you have a new file news.org/modules/news/forms/newsform.form.xml :

<?xml version="1.0" encoding="utf-8"?>
<form xmlns="http://jelix.org/ns/forms/1.0">
 
    <input ref="subject" type="string">
       <label>subject</label>
    </input>
 
    <input ref="text" type="string">
       <label>text</label>
    </input>
 
    <input ref="news_date" type="date">
       <label>news_date</label>
    </input>
 
   <submit ref="submit">
      <label>Save</label>
   </submit>
 
</form>

The generated form is very basic and most of time you have to modify it because the script cannot guess what you want exactly. For example, here, you have to modify the labels to have better labels. Perhaps you want other controls.. And here, we want also to add some constraints and help.

<?xml version="1.0" encoding="utf-8"?>
<form xmlns="http://jelix.org/ns/forms/1.0">
 
    <input ref="subject" type="string" required="true">
       <label>Subject of the news</label>
    </input>
 
    <input ref="text" type="string" required="true">
       <label>Content</label>
    </input>
 
    <input ref="news_date" type="date" required="true">
       <label>Date</label>
       <hint>The format is aaaa-mm-jj</hint>
    </input>
 
   <submit ref="submit">
      <label>Save</label>
   </submit>
 
</form>

Now the form is ready. We could use the jForms API to manipulate the form, but the controller we're going to use do it for us.

Creating a CRUD controller

Create the file modules/news/controllers/admin.classic.php, and start to write the controller:

<?php
 
class adminCtrl extends jControllerDaoCrud {
 
}
 
?>

Here the controller doesn't inherits from jController, but from jControllerDaoCrud. This is a controller which contains predefined actions to list, create, modify, save and delete records. So you just have to fill some properties to have a full management of the news.

A minimal setup is to indicate the dao and the form to use in the controller:

<?php
 
class adminCtrl extends jControllerDaoCrud {
 
    protected $dao = 'news~news';
 
    protected $form = 'news~newsform';
 
}
?>

Now try to display the main action of the controller: http://localhost:8080/index.php/news/admin/

You should see a list of records, links to edit/remove them or to add new ones.

Customizing the CRUD controller

If we want to modify some properties of the main HTML response, which is in our case myHtmlResponse, we can modify the method _getResponse of jControllerDaoCrud.

<?php
 
class adminCtrl extends jControllerDaoCrud {
 
    protected $dao = 'news~news';
    protected $form = 'news~newsform';
 
    protected function _getResponse(){
        $rep = $this->getResponse('html');
        $rep->title = "News manager";
        return $rep;
    }
}
?>

We can also redefine template for each page of the CRUD controller. By default, it uses some templates provided by the jelix module.

So let's create a new template to display the list of news. We start by copying the original one, vendor/jelix/jelix/lib/jelix/core-modules/jelix/templates/crud_list.tpl, to news.org/modules/news/templates/. And then we modify it, just the title for the moment:

  <h1>News management</h1>
...

You indicate then this new template to jControllerDaoCrud in the property $listTemplate:

<?php
 
class adminCtrl extends jControllerDaoCrud {
 
    protected $dao = 'news~news';
    protected $form = 'news~newsform';
 
    protected $listTemplate = 'news~crud_list';
...
}
?>

Now, if you relaunch http://localhost:8080/index.php/news/admin/, you should see your new list of news.

You can do the same thing for templates used to display the form etc. See the documentation of the CRUD controller.

Note

We created the form, the dao, and the controller separately to learn the framework. But we could also create all of them with a single command:

  php dev.php module:create-dao-crud <module> <table> [<ctrlname>]
 
    # so here:
 
    php dev.php module:create-dao-crud news news admin

It creates the controller, the dao and the form file.

Then you can see directly the manager: http://localhost:8080/index.php/news/admin/

en/tutorials/jelixnews-1.7/crud.txt · Last modified: 2019/05/23 11:51 (external edit)
Recent changes RSS feed Creative Commons License