Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用匹配的字段集自定义呈现ZF2集合_Php_Zend Framework_Zend Framework2 - Fatal编程技术网

Php 如何使用匹配的字段集自定义呈现ZF2集合

Php 如何使用匹配的字段集自定义呈现ZF2集合,php,zend-framework,zend-framework2,Php,Zend Framework,Zend Framework2,我已经创建了一个标准应用程序,它使用集合来映射一对多关系(不使用原则),与中描述的方式基本相同,我有如下代码: class Person { protected $attrributes; } class Attribute { protected $attr1; protected $attr2; } 我已经创建了AttributeFieldset和所需的匹配AttributeForm,并且在我的PersonForm上添加了AttributeFieldset: $this-&

我已经创建了一个标准应用程序,它使用集合来映射一对多关系(不使用原则),与中描述的方式基本相同,我有如下代码:

class Person {
  protected $attrributes;
} 

class Attribute {
  protected $attr1;
  protected $attr2;
}
我已经创建了
AttributeFieldset
和所需的匹配
AttributeForm
,并且在我的
PersonForm
上添加了
AttributeFieldset

$this->add(
  array(
    'type' => 'Zend\Form\Element\Collection',
    'name' => 'attributes',
    'options' => array(
      'label' => _("Add person attribute"),
      'count' => 1,
      'should_create_template' => true,
      'allow_add' => true,
      'target_element' => array(
        'type' => 'Persons\Form\AttributesFormFieldset'
      )
    )
  ));
调用
$this->formCollection()
视图帮助程序将生成集合和数据模板的默认HTML,以便按照文档中的指定通过javascript动态添加新属性

然而,我想要完成的是,用一个edit/delete选项创建一个包含person所有现有属性的表,并使用collection字段集创建一个模式窗口,以向person添加新属性

想象一下下面的html:

<a href="#" onclick="add(this); return false;">Add new attribute</a>
<table>
  <? foreach( $this->person->attributes as $attribute ): ?>
    <tr>
      <td><?= $attribute['attr1']; ?></td>
      <td>
        <a href="#" onclick="edit(this); return false">Edit</a> | <a href="#" onclick="delete(this); return false">Delete</a>
      </td>
    </tr>
  <? endforeach; ?>
</table>

| 
我知道我可以完全跳过formCollection,在我添加到表中的每一行上添加ZF2 Collection所期望的
标记(例如
属性[0][attr1]
等),并动态创建表单,但我猜我将错过ZF2 InputFilters


有没有更好的方法来实现这一点?以前有人这样做过吗?

处理集合并要求自定义标记很烦人。但这并不难:

$collections = $form->get('collection-element');
echo '<table>'
// render thead and tbody open if needed
foreach ($collections as $col) : ?>
    <tr>
        <td><?php echo $this->formInput($col->get('input-from-collection-name'); ?></td>
        <td><?php echo $this->formInput($col->get('other-input-from-collection-name'); ?></td>
    </tr>
<?php endforeach;
echo '</table>';
$collections=$form->get('collection-element');
回声“
//如有必要,将AD和T车身打开
foreach($col形式的集合):?>

你可能是对的,虽然FormCollection为我们提供了一个强大的功能,但它确实是一个麻烦。。我曾想过让模板也是静态的,但它失去了在下划线数据库发生更改时只更改字段集的所有好处Hello Sam,有没有传递模板参数的方法?因为我找不到创建的模板。我需要用它来创建额外的字段,比如这里@MyselfMalay,可惜没有这样的东西。在这些场景中,我仍然发现自己只需覆盖默认的FormRow/FormSelect/FormInput helpers。这样,字段集渲染通常适合我。另一种方法是使用FormSelect/FormInput等。。不使用formRow,然后使用字段集中的自定义HTML添加模板标记。。。这很烦人,但这正是web表单的含义:'(所以基本上这是不受支持的,我使用boostrap,无法为输入元素添加类,例如表单输入。我花了2天时间使用它,但没有运气。现在,有没有办法在自定义过程中获得生成的模板?@MyselfMalay添加css类应该一点问题都没有,因为它们属于
属性
表单元素的配置数组。此外,还有许多Twitter引导解决方案模块。