1.11. Edit One Article

We need to be able to edit the existing articles and mark them for publication.

1.11.1. The "Edit" Action Method

Open the Blog.php page controller class file. (In this example we use the vim editor, but you can use anything you like.)

$ vim Blog.php

Add the following PHP code to the class.

    public function actionEdit($id = null)
        // was an ID specified?
        if (! $id) {
            return $this->_error('ERR_NO_ID_SPECIFIED');
        // fetch one blog article by ID
        $this->item = $this->_model->blogs->fetch($id);
        // did the blog article exist?
        if (! $this->item) {
            return $this->_error('ERR_NO_SUCH_ITEM');
        // did the user click the save button?
        if ($this->_isProcess('save')) {
            // look for $_POST['blog'] in the request,
            // load into the record, and save it.
            $data = $this->_request->post('blog');
        // get form hints from the record
        $this->form = $this->item->newForm();
        // turn off http caching

[Note] Controller, Action, and Process

We already know about controller classes and action methods. However, an action method may handle different processes, based on which submit button was clicked in a form. The submit button pressed is called the "process" being requested of the action; the value is passed by default as $_POST['process'].

Some explanation of what's going on in this method:

  • First, we attempt fetch the requested blog article by its ID; if it doesn't exist, we show an error.

  • Next, we look to see if the user has clicked the 'save' button by checking _isProcess(). If so, we load the data from $_POST['blog'] into our record, and attempt to save it.

  • Finally, we get a set of form hints from the record and retain them in $this->form. If the save failed, the form hints will have a series of feedback messages to tell us what went wrong.

1.11.2. The "Edit" View Script

Create a view called edit.php for the actionEdit() method.

$ vim Blog/View/edit.php

Enter the following code in the new edit.php view script.

    $title = $this->getTextRaw('TITLE_EDIT');

<h2><?php echo $this->getText('HEADING_EDIT'); ?></h2>

    echo $this->form()

Essentially, this view takes the form hints ($this->form) and auto-generates a form for the user to work with.

[Note] Where's the Form Code?

The form() helper for Solar views takes the form hints stored in $this->form and auto-generates a form for you. (These hints came from the record object; for example, the <select> pulldown is pre-populated with "draft" and "public" options because of the validateInList filter we added in the model _setup() method.

However, there are no buttons in the form hints by default. To remedy this, we add a submit button using the addProcess() method on form() helper. That way the action method will know which process is being requested.

There are lots of other methods in the form() helper. You can build forms semi-automatically using it, or you can build them manually using the other form-element helpers. For more information, you can review the Solar_View_Helper_Form class documentation.

You should now be able to browse to http://localhost/index.php/blog/edit/2 and edit the article.