【问题标题】:How to Extend a Fieldset in ZF2如何在 ZF2 中扩展字段集
【发布时间】:2014-04-23 09:58:53
【问题描述】:

Doctrine 的类表继承映射策略涉及将父表与多个子表之一连接,具体取决于父表中鉴别器列中的值。例如,父表可能包含列 a、b 和 c; c 列中的值为foobar。名为“foo”的子表可能包含 d、e、f 和 g 列;而名为“bar”的子表可能包含列 p、q、r 和 s。为父级定义了一个实体,为每个子级定义了一个单独的实体(“foo”和“bar”)。在继承映射策略中,子实体“扩展”了父实体,因此子实体不需要重新定义父实体中的元素。

我的问题是,我们也可以“扩展”子字段集吗? “foo”字段集将由元素 a、b、c、d、e、f 和 g 组成,“bar”字段集将由元素 a、b、c、p、q、r 和 s 组成。我们真的需要多次定义元素 a、b 和 c 的选项和属性吗?这样做会增加代码量,并且需要努力确保在每个“foo”和“bar”中定义相同的 a、b 和 c。

【问题讨论】:

    标签: doctrine zend-framework2


    【解决方案1】:

    简短的回答是是的你可以。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多