表中的ZEND表单元素还包含来自数据库的数据 [英] ZEND form elements in a table containing also data from the database

查看:67
本文介绍了表中的ZEND表单元素还包含来自数据库的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里:)我在表中有装饰器和表格的问题,在此表中还要从数据库中获取数据...我不知道如何执行此操作,以使结构像下面有话要说

<table>
<tr>
  <td><?php echo array[0]['name']?>
//and here input from zend form
  <td>
  <select name='foo' id='bar'>
    <option value='something'>Foo</option>
    <option value='something2'>Foo2</option>
  </select>
  </td>
</tr>
</table>

当然,tr会更多,并通过foreach或某些循环生成.

我有这样的东西:

<?php

class EditArticles_Form_EditArticles extends Zend_Form
{
protected $uid;

public function render()
{


    /* Form Elements & Other Definitions Here ... */
    $this->setName('editarticles');

    $data = new EditArticles_Model_DbTable_EditArticlesModel();
    $datadata = $data->GetArticlesToEdit($this->getUid());    //here is my data from db


    for ($i=0;$i<count($datadata);$i++)
    {           
        $do = new Zend_Form_Element_Select(''.$i);
        $do->addMultiOption('0', 'Aktywny');
                $do->addMultiOption('1', 'Nieaktywny');

        $this->addElements(array($do));
    }



    $submit = new Zend_Form_Element_Submit('updateart');
    $this->addElement($submit);


    //and here are decorators for array, and i would like to have in this table also data from array containing data from database
    $this->addDecorators(array(

                        'FormElements',
                        array('HtmlTag', array('tag' => 'table', 'id' => 'aaaa', 'style' => 'width:500px;')), 'Form', 

                        ));                 

    $this->setElementDecorators(array(

                        'ViewHelper',
                        array(  array('data' => 'HtmlTag'), array('tag' => 'td', 'style' => 'width:200px;')), 
                                array('Label', array('tag' => 'td')),

                        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))

                        ), 
                        //wykluczenie submita z overrida stulu
                        array('submit'), false);


    return  parent::render();

}


//setting user id for get content from db
public function setUid($uid) {
    $this->uid = $uid;
    return $this;
}

public function getUid() {
    return $this->uid;
}

}
?>

上面的代码的

输出是这样的:(用红色标记表示我希望从表单中选择的位置.在此图像中,带有数据的表是phtml中生成的另一个表,但是我想生成该表按表单od,只需将表单元素插入到在phtml视图中生成的表中即可.

http://img14.imageshack.us/img14/9973/clipboard01pw.png

在这里找到的东西: Zend_Form:HTML表格中带有复选框的数据库记录

但是我不知道如何开始...

解决方案

几个评论:

  1. 通常,将元素添加到表单是在init()中完成的,而不是在render()中完成.

  2. 如果使用者对象(在这种情况下为表单)需要依赖项(在这种情况下为商品模型)来执行其工作,则明确地向使用者提供依赖项通常是有帮助的,或者在使用者的构造函数中或通过setter方法(例如:$form->setArticleModel($model)).这使得在测试表单时更容易模拟模型,并清楚地说明了表单对模型的依赖性.

  3. 回复:通过装饰器在表单中呈现其他内容:也许,请看一下http://img14.imageshack.us/img14/9973/clipboard01pw.png

    Something found here: Zend_Form: Database records in HTML table with checkboxes

    but i dont know how to start with that...

    解决方案

    Several comments:

    1. Typically, adding elements to the form is done in init(), rather than render().

    2. If a consumer object (this is this case, the form) needs a dependency (in this case, the article model) to do its work, it is often helpful to explicitly provide the dependency to the consumer, either in the consumer's constructor or via setter method (ex: $form->setArticleModel($model)). This makes it easier to mock the model when testing the form and clearly illustrates the form's dependence on the model.

    3. Re: rendering other content in the form via decorators: Maybe, take a look at the AnyMarkup decorator. It looks like (sorry, can't fully understand the Polish) you want a select box on each row you output. So, you get your rows using the model, loop through the rows, creating your select box on each row. When you assign decorators to the select element - ViewHelper, Errors, probably an HtmlTag decorator to wrap it in a <td> - you also add the AnyMarkup decorator to prepend the a bunch of <td>'s containing your row data, finally wrapping the whole row in <tr>.

    Perhaps something like this (not fully tested, just to give the idea):

    class EditArticles_Form_EditArticles extends Zend_Form
    {
        protected $model;
    
        public function __construct($model)
        {
            $this->model = $model;
            parent::__construct();
        }
    
        public function init()
        {
            $rows = $this->model->GetArticlesToEdit($this->getUid());
            $numRows = count($rows);
            for ($i = 0; $i < $numRows; $i++) {
                $do = new Zend_Form_Element_Select('myselect' . $i);
                $do->addMultiOption('0', 'Aktywny');
                $do->addMultiOption('1', 'Nieaktywny');
                $do->setDecorators(array(
                    'ViewHelper',
                    array(array('cell' => 'HtmlTag'), array(
                            'tag' => 'td'
                    )),
                    array('AnyMarkup', array(
                            'markup' => $this->_getMarkupForRow($i, $row),
                            'placement' => 'PREPEND',
                    )),
                    array(array('row' => 'HtmlTag'), array(
                            'tag' => 'tr'
                    )),
                ));
                $this->addElement($do);
            }
        }
    
        protected function _getMarkupForRow($i, $row)
        {
            return '<td>' . $i . '</td>' .
                '<td>' . $row['nazwa'] . '</td>' .
                '<td>' . $row['typ'] . '</td>' .
                '<td>' . $row['rozmiar'] . '</td>';
        }
    }
    

    A final note: Remember to register an element decorator prefix path as follows (in the form, probably in init()):

    $this->addElementPrefixPath('My_Decorator', 'My/Decorator', self::DECORATOR);

    This allows the element to resolve the short name AnyMarkup into a full classname My_Decorator_AnyMarkup.

    这篇关于表中的ZEND表单元素还包含来自数据库的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆