简体   繁体   English

Zend表单字段映射到数据库列

[英]Zend Form Field Mapping To DB Columns

I have a Zend_Form 我有一个Zend_Form

$form = new My_Form();
$uploadedData = $form->getValues();
Zend_Debug::dump($uploadedData, $form->name,true)

and when i dump the form values i get a string array 当我转储表单值时,我得到一个字符串数组

array(11) {
  ["event"] => string(2) "26"
  ["firstname"] => string(3) "sdf"
  ["surname"] => string(0) ""
  ["gender"] => string(1) "M"
  ["company"] => NULL
  ["dateOfBirth"] => string(10) "11-11-1977"
  ["address"] => string(6) "dfasdf"
  ["address2"] => string(4) "adfs"
  ["address3"] => string(4) "adfs"
  ["email"] => string(7) "x@x.com"
  ["mobile"] => string(0) ""
}

The form has the same number of fields as my DB table, the DB table def is 表单具有与我的数据库表相同的字段数,数据库表def为

DROP TABLE IF EXISTS `registration`;
CREATE TABLE IF NOT EXISTS `registration` (
  `id` int(11) NOT NULL auto_increment,
  `event` int(11) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `surname` varchar(50) NOT NULL,
  `gender` varchar(5) NOT NULL,
  `dateofbirth` date NOT NULL,
  `address` varchar(50),
  `address2` varchar(50),
  `address3` varchar(50),
  `email` varchar(50) NOT NULL,
  `mobile` varchar(50),
  `company` int(11),
  `sector` int(11),
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I'm getting a mapping error at the moment, since the DB event column can't take a string, and same with the 'dateofbirth' column 由于数据库事件列无法接受字符串,并且与“ dateofbirth”列相同,因此我目前遇到映射错误

if($form->isValid($formData))
{
    $logger->info("valid form");
    $table = new Model_DbTable_Registration();
    ... // apply some custom mapping??
    $table->insert($uploadedData);
    ...    
}

I don't want to have to manually create a new array, and map each form field to the correct type by name - it seems like a pain. 我不想手动创建一个新数组,并按名称将每个表单字段映射到正确的类型-似乎很痛苦。 Is there a smarter way to do this mapping in Zend? 在Zend中是否有更聪明的方法来进行此映射?

That is why on the new ZF version is using the DataMapper Pattern for things like this, you abstract completely how to set, save and retrieve the data. 这就是为什么在新的ZF版本上使用DataMapper模式进行此类操作时,您完全抽象了如何设置,保存和检索数据。

Your best bet is to create a new class and then interact with that class, and internally that class interact with your DbTable class. 最好的选择是创建一个新类,然后与该类进行交互,然后在内部与您的DbTable类进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM