开发者

Zend Framework: Fill form with data from mapper

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();

开发者_C百科    //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)?


public function init() {

    $model = new News_Model_Backend();
    $this->populate($model->toArray());
...


Use this:

$form->populate($model->find($id)->current()->toArray());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜