用对象中的对象填充表单's字段集


populating zend form's fieldsets with objects within object

我使用PHP Zendframework来构建表单。我有服务对象,我需要用它来填充我的ServiceEditForm.php。但是在这个服务表单中,我有对象"Billing","Subscription"和对象数组"Commands"。下面是我的Service类实现。

 class Service{
public $service_id;
public $ServiceName;
public $TelecomOperator;
public $SubMethod;
public $Provider;
public $active;

public $billingType;
public $subscriptionPlan;
public $commands;
function exchangeArray(array $data);}

我想将Service类的对象绑定到使用订阅、计费和命令相关数据作为字段集的Edit表单。我可以使用bind而不是其他对象来填充服务值。这是我的表单实现

     class ServiceEditForm extends Form{
public function __construct($name = null)
{
    parent::__construct('Edit Service');
    $this->setAttribute('method', 'post');
    $this->setAttribute('enctype','multipart/form-data');
    //here i have other fields that belongs to service object
    $this->add( array(
        'name' => 'billingType',
        'type' => 'Services'Form'BillingTypeFieldset',
        'options' => array(
            'label' => 'Billing Type',
        ),
    ));

    $this->add( array(
        'name' => 'subscriptionPlan',
        'type' => 'Services'Form'SubscriptionPlanFieldset',
        'options' => array(
            'label' => 'Subscription Plan',
        ),
    ));
    $this->add(array(
         'type' => 'Zend'Form'Element'Collection',
         'name' => 'commands',
         'options' => array(
             'label' => 'commands',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => array(
                 'type' => 'Services'Form'CommandFieldset',
             ),
         ),
     ));
    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Save'
        ),
    )); 
}

}

正如我所说的,我不能用这个实现来填充字段集。

虽然我已经在寻找可能的解决方案后回答了这个问题,但这个链接中给出的Hydrator实现也有助于解释在使用它之前应该阅读的基本知识。我的错,在实现字段集之前,我没有进行全面的研究。

http://framework.zend.com/manual/current/en/modules/zend.stdlib.hydrator.html

这个url解释了hydrators的可能实现,它可以为你的类提供非常简单的绑定解决方案。