views:

62

answers:

1

There is an example of getting data from form using mapper in official quickstart. But there is no example in the whole Internet of populating form with data from mapper (for usual edit action, for example).

I usually do something like this (without using mapper and dbTable):

class News_Model_Form_News extends Zend_Form {

private $id = 0;

public function getId(){
    return $this->id;
}

public function setId($id){
    $this->id = $id;
}

public function init() {

    $model = new News_Model_Backend();

    //if form is needed for adding action
    if (!$this->id) {
        //form code
    } else {
    //if form is needed for edit action (fill with values)
        //form code with values
    }
}}

But with mapper pattern this stuff should became more elegent and cool, but I don't know how to do this.

So the question is how to populate form with values using mapper pattern (for edit guestbook entry from quickstart, for example)?

+2  A: 
public function init() {

    $model = new News_Model_Backend();
    $this->populate($model->toArray());
...
SM
To expand: http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.elements.values
Ashley