如何为 ManyToOne 学说映射设置 zf2 表单



我在使用 ManyToOne 单向关系在 zf2 中水化表单时遇到了麻烦。 我的实体如下所示:

namespace AdminMyPagesEntity;
class MyPageItem
{
    // ... 
    /**
     * @ORMManyToOne(targetEntity="MyMessage")
     * @ORMJoinColumn(name="myMessageID", referencedColumnName="myMessageID")
     **/
    private $myMessage;
    // ... 
    /**
     * Set MyMessage
     *
     * @param Collection $myMessage
     */
    public function setMyMessage(Collection $myMessage = null)
    {
        $this->myMessage = $myMessage;
    }
    /**
     * Get MyMessage
     * 
     * @return myMessage
     */
    public function getMyMessage()
    {
        return $this->myMessage;
    }
}
class MyMessage
{
    // ... 
}

MyPageItemFieldset的字段集如下所示:

namespace AdminMyPagesForm;
class MyPageItemFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('mypage-item-fieldset');
        $this->setHydrator(new DoctrineHydrator($objectManager, 'AdminMyPagesEntityMyPageItem'))
             ->setObject(new MyPageItem());
        // ...
        $myMessageFieldset = new MyMessageFieldset($objectManager);
    }
    public function getInputFilterSpecification()
    {
        // ...
        return array(
            'myMessage' => array(
                'required' => false
            ),
        );
    }
}

MyMessageFieldset的字段集如下所示:

namespace AdminMyPagesForm;
use DoctrineCommonPersistenceObjectManager;
use DoctrineModuleStdlibHydratorDoctrineObject as DoctrineHydrator;
use ZendFormFieldset;
use ZendInputFilterInputFilterProviderInterface;
use AdminMyPagesEntityMyMessage;
class MyMessageFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('mypage-message-fieldset');
        $this->setHydrator(new DoctrineHydrator($objectManager, 'AdminMyPagesEntityMyMessage'))
             ->setObject(new MyMessage());
        // ...
        'name' => 'myMessageText',
            'type' => 'ZendFormElementTextarea',
            'attributes' => array(
                //'type'  => 'textarea',
                'rows'  => 10,
            ),
            'options' => array(
                'label' => 'text',
            ),
        ));
        // ...
    }
    public function getInputFilterSpecification()
    {
        // ...
        return array(
            'myMessageText' => array(
                'required' => false
            ),
        );
        // ...
    }

}

在我的控制器中,我有:

    $myPageItem = $this->getEntityManager()->find('AdminMyPagesEntityMyPageItem', $mypageitemID);
    $form = new EditMyPageItemForm($objectManager);
    $form->setBindOnValidate(false);
    $form->bind($myPageItem);

通过这种配置,我能够从MyMessagegetMyMessage()"获取"数据,因此我知道表已连接。 我可以在表单中绑定实体,并使用 MyPageItemFieldset 中的表单元素。 但是,我无法使用 MyMessageFieldset 中的表单元素。 我不知道我的问题是否在于我如何编写字段集文件,或者是否在于我尝试调用表单元素的方式。 以下是我在编辑视图中进行的一些试验:

// this gets the message text that can be displayed in the view:
$myMessageText = $myPageItem->getMyMessage()->getMyMessageText();
// this allows me to get a form element from the MyPageItemFieldset:
$pifs=$form->get('mypage-item-fieldset');
$myPageItemOwner = $pifs->get('myPageItemOwner');
// these are some trails for getting a form element from the MyMessageFieldset:
$mfs_1 = $pifs;
$mfs_2 = $pifs->get('myMessageFieldset');
$mfs_3 = $pifs->get('');
$mfs_4a = $pifs->get('myMessageFieldset')->getFieldsets();
$mfs_4b = $mfs_4a[0];
$mfs_5 = $pifs->$myMessageFieldset->get('mypage-message-fieldset');
    $myMessageText = $mfs_1->get('myMessamypage-message-fieldsetgeText');
    // No element by the name of [myMessageText] found in form
    $myMessageText = $mfs_2->get('myMessageText');
    // No element by the name of [myMessageFieldset] found in form
    $myMessageText = $mfs_3->get('myMessageText');
    // No element by the name of [mypage-message-fieldset] found in form
    $myMessageText = $mfs_4b->get('myMessageText');
    // No element by the name of [myMessageFieldset] found in form  
    $myMessageText = $mfs_5->get('myMessageText');
    // Notice: Undefined variable: myMessageFieldset in  ... edit.phtml ...

你看过 DoctrineModule Hydrator 文档吗? IMO有点放错地方(与DoctrineORMModule有关,但存储在DoctrineModule中),所以如果你没有,我不会感到惊讶。 该部分包含为具有 ManyToOne 关联的实体构建完整表单基础结构的完整示例。

相关内容

  • 没有找到相关文章

最新更新