Question

I have a 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

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

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?

Was it helpful?

Solution

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.

Your best bet is to create a new class and then interact with that class, and internally that class interact with your DbTable class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top