2009年3月28日星期六

Re: [fw-mvc] form->getValues and file upload

On Sat, Mar 28, 2009 at 1:40 PM, Thomas Weidner <thomas.weidner@gmx.at> wrote:
> Ed,
>
> I don't quite understand what you mean because I replied to Simone.
> You never sent a line of code and I was just replying to the extension of 2
> base classes from Simone which is not necessary.

Sorry, I see now that it went just to Simone, but here it is now.

-Ed

If it helps, here's more details on what originally brought this up.

I was processing a form that included a file upload. Most of the data
from the form was going to be used in creating a new database record.
The final name of the uploaded file was going to include the ID of the
new database record. Something like this:

if ($form->isValid($request->getPost()) {
$application = new Application();
// save new record
$id = $application->saveEntry($form->getValues());

// use new id to process and save attached file...
$upload = new Zend_File_Transfer();
$upload->receive();
// more processing, etc...
}


Receiving the uploaded files would fail, because $form->getValues()
had already been called and the files were no longer in the tmp
directory (which is odd, IMHO).

My solution was something like this:

if ($form->isValid($request->getPost()) {
$application = new Application();
// save new record
$data = array();
$data['field1'] = $form->getValue('field1');
... extra lines for building the data array
$id = $application->saveEntry($data);

// use new id to process and save attached file...
$upload = new Zend_File_Transfer();
$upload->receive();
// more processing, etc...
}


IMHO, I consider that a work around solution, because I should be able
to just call $form->getValues(). Another option would be to receive
the files before saving the entry. I honestly didn't try that
approach though. I just put the work around in place until I could
figure out what was going on with the getValues method, because I
first thought I was doing something wrong or perhaps there was a bug
in getValues.

What do you think?

Thomas, is this helpful at all?

-Ed

没有评论: