Development

DatabaseFileStorageSystem (diff)

You must first sign up to be able to contribute.

Changes between Version 5 and Version 6 of DatabaseFileStorageSystem

Show
Ignore:
Author:
rekabis (IP: 24.70.190.21)
Timestamp:
07/22/08 23:36:24 (9 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DatabaseFileStorageSystem

    v5 v6  
    2121 * hard to export to SQL scripts (best way to transport is to zip the db folder and extract it to another name) 
    2222 * hard to put testing data into fixtures 
     23 * when using the sfAdvancedAdminGeneratorPlugin along with the Generator, the plugin alters the auto-generated class files to the extent that it prevents this method from functioning as described. Please see the section just prior to the conclusion for a workaround. 
    2324 
    2425== Disclaimers == 
    386387It isn't great that we need to modify our httpd.ini file every time we want to put a dot (.) in the query string in IIS, but this is the best I've come up with that can still hide the front controller. Any other ideas are MORE than welcome. 
    387388 
     389 
     390 
     391== Working with sfAdvancedAdminGeneratorPlugin == 
     392 
     393Because the sfAdvancedAdminGeneratorPlugin significantly changes how the Generator creates the class files in the cache, the method above does not function. The files end up being saved to the file system before there is a chance to intervene. As such, the files must be read out from the file system and then deleted from the file system (to avoid cruft). 
     394 
     395When working with sfAdvancedAdminGeneratorPlugin, it creates a function called executePost. This is where you want to work with your files. Copy this function out of the cache class file, and paste it into your permanent class file. The raw function should look something like this: 
     396 
     397{{{ 
     398  public function handlePost()  { 
     399    $this->updatesfGuardUserCompanyFromRequest(); 
     400    $this->savesfGuardUserCompany($this->sf_guard_user_company); 
     401    $this->setFlash('notice', 'Your modifications have been saved'); 
     402    if ($this->getRequestParameter('save_and_add')){return $this->redirect('sfGuardUserCompany/create');} 
     403    else if ($this->getRequestParameter('save_and_list')){return $this->redirect('sfGuardUserCompany/list');} 
     404    else{return $this->redirect('sfGuardUserCompany/edit?id='.$this->sf_guard_user_company->getId());} 
     405  } 
     406}}} 
     407 
     408At this point, once the second line ($this->update[module]FromRequest();) has been executed, the files have been saved to the file system, given random strings as names, and the new filenames are then stored in the entry which is supposed to hold your file data, but isn’t. Create another function, like the one below: 
     409 
     410{{{ 
     411  public function catchImage($image){ 
     412    if($image){ 
     413      $filename=sfConfig::get('sf_upload_dir').'/'.$image; 
     414      $data=fread(fopen($filename, "r"), filesize($filename)); 
     415      unlink($filename); 
     416      return $data; 
     417    } 
     418  } 
     419}}} 
     420 
     421The whole point here is that we want to read out the filename stored in the entry, grab the image from the filesystem, read it out, and then return it to the entry; overwriting the filename with the file data. As a bonus, we delete (unlink) the file, so that it doesn’t clutter up the file system. 
     422 
     423Then, in the handlePost function, we add a new line that will call this function: 
     424 
     425{{{ 
     426    $this->sf_guard_user_company->setLogo($this->catchImage($this->sf_guard_user_company->getLogo())); 
     427}}} 
     428 
     429As you can see when reading that line from right to left, the new filename of the image now saved to the filesystem is obtained, passed on to the catchImage function, and the catchImage function returns the image data back to the entry where it is set and overwrites the filename. Past this point, symfony handles the image data in the same way it would have handled the filename. 
     430 
     431The completed functions should appear something like this: 
     432 
     433{{{ 
     434  public function handlePost(){ 
     435    $this->updatesfGuardUserCompanyFromRequest(); 
     436 
     437    $this->sf_guard_user_company->setLogo($this->catchImage($this->sf_guard_user_company->getLogo())); 
     438 
     439    $this->savesfGuardUserCompany($this->sf_guard_user_company); 
     440    $this->setFlash('notice', 'Your modifications have been saved'); 
     441    if ($this->getRequestParameter('save_and_add')){return $this->redirect('sfGuardUserCompany/create');} 
     442    else if ($this->getRequestParameter('save_and_list')){return $this->redirect('sfGuardUserCompany/list');} 
     443    else{return $this->redirect('sfGuardUserCompany/edit?id='.$this->sf_guard_user_company->getId());} 
     444  } 
     445  public function catchImage($image){ 
     446    if($image){ 
     447      $filename=sfConfig::get('sf_upload_dir').'/'.$image; 
     448      $data=fread(fopen($filename, "r"), filesize($filename)); 
     449      unlink($filename); 
     450      return $data; 
     451    } 
     452  } 
     453}}} 
     454  
    388455== Conclusion == 
    389456