Howto upload an image and store a permalink to it in the database

Notice: This thread is very old.
mudasobwa
Member | 2
+
0
-

Hi all,

I guess I miss smth very evident, but… So, the problem goes: I want to upload an image to the server using Nette\Forms\Controls\UploadControl, move it to some uploads folder outside of the server root and immediately store it’s location in the database.
I can simply use two requests (in pseudocode):

<?php
$row = $this->mytable->insert($form->values);
// Deal with file
$row->update(array('image' => $filePermalink));
?>

But I’m pretty sure there is more elegant way to perform the task.

Although, what am I missing and where am I a butt-head?

Tnx in advance.

Last edited by mudasobwa (2012-01-23 09:22)

petr.pavel
Member | 535
+
0
-

Hi mudasobwa,
if you need the new record's id for the permalink then it's a chicken-egg problem.
You have to create the db record prior to generating the permalink.

If your permalink however, does not contain the db record id then you can pass it to insert() right away:

<?php
$values = $form->values;
$values['image'] = $filePermalink;
$row = $this->mytable->insert($values);
?>
mudasobwa
Member | 2
+
0
-

Hi, petr.pavel!

Thanks, that's exactly what I need. I have tried the direct

<?php
  $form['image'] = $filePermalink;
?>

the framework prevented the form variable change and I screwed up for a little.