The CRUD plugin provides a basic implementation of the CRUD pattern. It’s meant to be used behind a form to apply various checks on the data.

This plugin is basic but stand-alone; for more advanced features, you can use the RedBeanORM plugin, a wrapped around the excellent RedBean open source library.

Usage

The typical usage of CRUD is inside a controller managing a form. In our example, the html is generated by Smarty, as in the model-view-controller pattern.

The skeleton is as this:

  1. instansiate crud
  2. if it’s a form save call, check the data
  3. then if the data are ok, do the action (actually save the data to the database, usually)
  4. display the form

In the last step, the form will be displayed and adjusted according to the situation: if some fields are in error, they will be displayed as such.

Here is the code:

    class MyFormController extends PHPDS_controller
    {
        public function execute()
        {
            /* @var $crud crud */
            $crud = $this->factory('crud');
            if ($crud->POST('save')) {
                $crud->addField('some_field');
                // some data test
                if (!$crud->isAlphaNumeric('some_field')) {
                    $crud->error(_('The field "some field" should be alphanumeric'));
                }
                if ($crud->ok() {
                    $this->do_action($crud->f);
                } else $crud->errorShow();
            }
            $view = $this->factory('views');
            $view->set('some_field', $crud->f->some_field);
            $view->show();
        }
        public function do_action($data)
        {
                $some_field = $data->some_field;
        }
    }

Every fields added with $crud->addField($fieldname) will be available as a property (field) of the object $crud->f.

Réaction

Était-ce utile?

Oui Non
Vous avez indiqué que ce sujet ne vous a pas été utile ...
Pouvez-vous SVP laisser un commentaire nous disant pourquoi? Merci!
Merci pour vos commentaires.

Laissez votre avis sur ce sujet.

Valider