表中的 ZEND 表单元素还包含数据库中的数据

发布于 2024-12-17 03:22:10 字数 2814 浏览 2 评论 0原文

你好:)我有一个装饰器和表单的问题,它会在表中,并且在这个表中还希望有来自数据库的数据...我不知道如何做到这一点以获得如下所示的结构,可以说,

<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 中生成的另一个表,但我想通过表单生成该表,只需将表单元素插入到在 phtml 视图中生成的表中)。

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

在这里发现了一些东西: Zend_Form:带有复选框的 HTML 表中的数据库记录

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

Hi there:) i've got a problem with decorators and form which would be in table and in this table want to have also data from database... I dont have any idea how to do this to have a structure like something below, lets say

<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>

Ofcourse tr will be more and generated with foreach or some loop.

I have something like this:

<?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;
}

}
?>

output of code above is something like this: (in red marked where i would like to have that selects from form. In this image the table with data is an other table generated in phtml, but i would like to generate that table by form od just insert only the form elements to that table generated in phtml view).

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-12-24 03:22:10

几点评论:

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

  2. 如果消费者对象(在本例中为表单)需要依赖项(在本例中为文章模型)来完成其工作,则显式向消费者提供依赖项通常会很有帮助,无论是在消费者的构造函数或通过 setter 方法(例如:$form->setArticleModel($model))。这使得在测试表单时更容易模拟模型,并清楚地说明表单对模型的依赖关系。

  3. 回复:通过装饰器渲染表单中的其他内容:也许,看看 AnyMarkup 装饰器。看起来(抱歉,无法完全理解波兰语)您希望在输出的每一行上都有一个选择框。因此,您可以使用模型获取行,循环遍历行,在每行上创建选择框。当您将装饰器分配给选择元素时 - ViewHelperErrors,可能是一个 HtmlTag 装饰器将其包装在 - 您还添加 AnyMarkup 装饰器来添加一堆包含行数据的 ,最后将整行包装在.

也许是这样的(没有完全测试,只是为了给出想法):

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>';
    }
}

最后一点:记住注册一个元素装饰器前缀路径,如下所示(在形式中,可能在 init() 中):

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

这允许元素将短名称 AnyMarkup 解析为完整的类名My_Decorator_AnyMarkup

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文