如何在ZF2中扩展一个字段集 [英] How to Extend a Fieldset in ZF2

查看:117
本文介绍了如何在ZF2中扩展一个字段集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Doctrine的Class Table继承映射策略涉及将父表与多个子表之一相连,取决于父表中的标识符列中的值。例如,父表可能包含列a,b和c;列c中的值为 foo bar 。名为foo的子表可能包含列d,e,f和g;而名为bar的子表可能包含列p,q,r和s。为父项定义单个实体,并为每个小孩('foo'和'bar')定义单独的实体。在继承映射策略中,子实体扩展父实体,所以孩子不需要重新定义父元素。

Doctrine’s Class Table Inheritance mapping strategy involves joining a parent table with one of a number of child tables depending on the value in a discriminator column in the parent table. For example, a parent table might contain the columns a, b, and c; with the values in column c being either foo or bar. A child table named ‘foo’ might contain the columns d, e, f and g; while a child table named ‘bar’ might contain the columns p, q, r and s. A single entity is defined for the parent and a separate entity is defined for each child (‘foo’ and ‘bar’). In the inheritance mapping strategy, the child entities ‘extend’ the parent entity, so there is no need in the child to redefine elements in the parent.

我的问题是,我们可以延长孩子的田野吗? 'foo'字段将由元素a,b,c,d,e,f和g组成,'bar'字段将由元素a,b,c,p,q,r和s组成。我们真的需要多次定义元素a,b和c的选项和属性?这样做增加了代码量,并且需要勤勉确保在每个foo和bar中确定a,b和c是相同的。

My question is, can we ‘extend’ the child fieldsets as well? The ‘foo’ fieldset is going to consist of elements a, b, c, d, e, f and g and the ‘bar’ fieldset is going to consist of elements a, b, c, p, q, r and s. Do we really need to define the options and attributes for elements a, b, and c more than once? Doing so multiplies the amount of code, and requires diligence in ensuring that a, b, and c are defined identically in every ‘foo’ and ‘bar’.

推荐答案

简短的答案是可以。

class FieldsetParent extends Zend\Form\Fieldset
{
   public function init() {
        $this->add(array('name' => 'fieldA'));
        $this->add(array('name' => 'fieldB'));
        $this->add(array('name' => 'fieldC'));
   }
}

class FieldsetFoo extends FieldsetParent
{
   public function init() {

        parent::init();

        $this->add(array('name' => 'fieldD'));
        $this->add(array('name' => 'fieldE'));
        $this->add(array('name' => 'fieldF'));
        $this->add(array('name' => 'fieldG'));
   }
}

class FieldsetBar extends FieldsetParent
{
   public function init() {

        parent::init();

        $this->add(array('name' => 'fieldP'));
        $this->add(array('name' => 'fieldQ'));
        $this->add(array('name' => 'fieldR'));
        $this->add(array('name' => 'fieldS'));
   }
}

这篇关于如何在ZF2中扩展一个字段集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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