从 Doctrine 实体构建 Zend_Form 的元素
经过大量阅读,我意识到 Zend_Form 可以分为各种表示形式,它非常适合作为模型的视图。
遵循 Matthew Weier O'Phinney,我想到了将 Doctrine [2] Entitie 传递给扩展 Zend_Form 的类的构造函数,在本例中是 App_Form。
因此,在我的实体中,我为每个表单都有一个方法,遵循以下模式:
protected function _formFormName{}()
Entitie 扩展了抽象类 App_Form_Entitie,它有一个检索表单的方法:
final public function getForm($form = null)
并且仍然具有方法 isValid() 和getMessage()。
但许多人更喜欢将表单保留在单独的文件中,请参阅:
在哪里放置 Zend_Forms、控制器?模型?在其他地方?
我想知道哪一个是最好的方法:将实体作为第一个参数传递给表单的构造函数,并将所需的表单(例如可选的第二个参数)传递给表单,或者从实体获取表单(正如马修所描述的)并传递所需形式的名称作为第一个参数。
欢迎任何答案。
下面是两个代码,显示了这两个示例的情况,首先是我的建议:
<?php
//My controller action
/* @var $em Doctrine\ORM\EntityManager */
$user = $em->find('MyNamespace\User', 1);
$loginForm = new App_Form($user, array('form'=> 'login'));
$this->view->loginForm = $loginForm;
//My view script
echo $this->loginForm;
这里是 Matthew 的建议,其中表单位于单独的文件中:
<?php
//My controller action
/* @var $em Doctrine\ORM\EntityManager */
$user = $em->find('MyNamespace\User', 1);
$formLogin = $user->getForm('login'); //The entity creates a new instance of the class App_Form_Login and returns it.
$this->view->formLogin = $formLogin
//In my view...
echo $this->formLogin;
After much reading, I realized that Zend_Form can be divided into various representations, it fits well in view as the model.
Following the logic proposed by Matthew Weier O'Phinney, I got an idea to pass Doctrine [2] Entitie to the constructor of a class that extends Zend_Form, in the case, App_Form.
So in my entitie, I have a method for each form, following the pattern:
protected function _formFormName{}()
that Entitie extends the abstract class App_Form_Entitie, that have a method to retrieve the form:
final public function getForm($form = null)
and still have the methods isValid() and getMessage().
But many people prefer to leave the forms in separate files, see:
Where to place Zend_Forms, Controller? Model? Somewhere else?
I wonder which of these is the best way: Pass the entity to the constructor of the form as the first parameter, and the desired form such a, optional, second parameter, or get the form from the entity (as Matthew describes) and pass as first parameter the name of the desired form.
Any answer is welcome.
Here are two codes showing how the two examples would be, first, my proposal:
<?php
//My controller action
/* @var $em Doctrine\ORM\EntityManager */
$user = $em->find('MyNamespace\User', 1);
$loginForm = new App_Form($user, array('form'=> 'login'));
$this->view->loginForm = $loginForm;
//My view script
echo $this->loginForm;
And here the proposal of Matthew, in which the forms are in separate files:
<?php
//My controller action
/* @var $em Doctrine\ORM\EntityManager */
$user = $em->find('MyNamespace\User', 1);
$formLogin = $user->getForm('login'); //The entity creates a new instance of the class App_Form_Login and returns it.
$this->view->formLogin = $formLogin
//In my view...
echo $this->formLogin;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
User
实体创建App_Form
对象似乎不正确。您的实体是您在应用程序的其他部分中引用的数据的理想化抽象。这并不妨碍您为表单类型创建单独的文件。我会选择像你的第一个选择这样的东西:It doesn't seem right for the
User
entity to createApp_Form
objects. Your entities are idealized abstractions of your data which you reference in other parts of your application. This doesn't prevent you from having separate files for your Form types. I would go for something like your first option: