Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some research:</p> <p>While you can extend sfValidatedFile and override generateFilename I found out that sfFormPropel checks for the existence of a method based on the column name for the model to name the file.</p> <p>From symfony/plugins/sfPropelPlugin/lib/form line 292:</p> <pre><code>$method = sprintf('generate%sFilename', $column); if (null !== $filename) { return $file-&gt;save($filename); } else if (method_exists($this, $method)) { return $file-&gt;save($this-&gt;$method($file)); } </code></pre> <p>Therefore, if your column is called file_name, the method looks for the existence of generateFileNameFilename in the form class. This way you only have to add one method to your form class, rather than extending the sfValidatedFile widget. For instance, my function uses the original name if it is not taken, otherwise appends a sequential number (one method is to recursively check the generated filename):</p> <pre><code>public function generateFileNameFilename($file = null) { if (null === $file) { // use a random filename instead return null; } if (file_exists($file-&gt;getpath().$file-&gt;getOriginalName())) { return $this-&gt;appendToName($file); } return $file-&gt;getOriginalName(); } public function appendToName($file, $index = 0) { $newname = pathinfo($file-&gt;getOriginalName(), PATHINFO_FILENAME).$index.$file-&gt;getExtension(); if (file_exists($file-&gt;getpath().$newname)) { return $this-&gt;appendToName($file, ++$index); } else { return $newname; } } </code></pre> <p>I can't find this documented in the symfony API anywhere which is why it took some searching the code base to find. If you are using this method in many places, extending sfValidatedFile might be a good option too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload