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 : a powerfull 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 jelix.php createform 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/jelix/news.org/www/index.php?module=news&action=admin:index

You should obtain… an empty page !

In fact, the controller use the default html response and set nothing on it since most of time you will define a personnalized html response, as it is indicated in this page.

Since we didn't develop a such response in the tutorial, we're going to redefine a method of the controller to prepare the response. This method is _getResponse, specific to jControllerDaoCrud.

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

Let's create the template modules/news/templates/admin_news.tpl:

  <h1>News management</h1>
 
  {$MAIN}
 
  <hr />
  <div><a href="{jurl 'news~default:index'}">Retour to homepage</a></div>

Now, if you relaunch http://localhost/jelix/news.org/www/index.php?module=news&action=admin:index, you should see a list of news, with links to modify, delete etc.

You can override the template to change them and adding informations or what you want.

Note

If you want to create a CRUD on a new table (so no DAO and forms on this table), you can create it in a single command:

  php jelix.php createdaocrud  the_module the_table_name

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

Then you can see directly the manager: site.com/index.php?module=the_module&action=default:the_table_name

en/tutorials/main/crud.txt · Last modified: 2012/04/15 08:33 by laurent
Recent changes RSS feed Creative Commons License