Code snippets for symfony 1.x


Snippets by user Roberto Carvajal

Create profile after adding a new user, the simple way.

Sometimes if you use sfGuardUser and sfGuardUserProfile you need to create an empty profile just after adding the new user (automating is such a great thing). This snippet will help out.

UPDATE: Will Killian pointed me to the solution of using this without touching the plugin files, this way it will still work after upgrading the plugin. Thanks!

Edit apps/backend/modules/sfGuardUser/actions/actions.class.php

(create directories and files as needed, just for the record I use this with admin generator on "backend" app)

Mine looks like this.

class sfGuardUserActions extends BasesfGuardUserActions
  protected function savesfGuardUser($sf_guard_user)
    $user_id = $sf_guard_user->getId();
    $c = new Criteria();
    $profile = sfGuardUserProfilePeer::doSelectOne($c);
    if(!$profile) {
      $profile = new sfGuardUserProfile();
by Roberto Carvajal on 2007-10-30, tagged profile  sfguard  user 

keep original filename when using admin generator upload

This is a simple way I came up for keeping original filenames for the files I upload using the admin generator.

We need an extra field in our database schema to store the original filename, lets say our file field it's called "lecture" then our original lecture's filename field would be "original_lecture", our object will be called "Course".

Remember to rebuild your object model before the next step.

Once we have the 2 fields there we need to ignore the second field inside generator.yml, the way to do this is to alter the display parameter and only list the fields you want to edit.

Then we overwrite our actions and add the following code:

class courseActions extends autocourseActions
  protected function updateCourseFromRequest()

This will automatically setup the original filename on the right field.

Now, we move to the frontend, we need an action to download our file so we can alter the request and add our filename on the fly, you can add the following code on any frontend action but for clarity sake I've created a module called "download" that I'll use to download any of my files.

So, inside our download module we add the following action:

class downloadActions extends sfActions
   * Executes lecture action
  public function executeLecture()
    $id = $this->getRequestParameter('id');
    $this->course = CoursePeer::retrieveByPk($id);
    $this->getResponse()->addHttpMeta('Content-Disposition', 'attachment; filename="'.$this->course->getOriginalLecture().'"');

As you can see, we added a Content-Disposition header to our request, this header will force to download a file and name it according to our original_lecture field.

Finally, on our view, we need to do 2 things.

1) Disable the layout, this is done under view.yml just add:

  has_layout: off

2) Inside lectureSuccess.php we add:


This will output the file name stored in the filesystem, remember to provide the right path, this is just an example :)

Finally, to download the file you just need to follow this link:

That's all, your browser will prompt you with a save file dialog.

by Roberto Carvajal on 2007-06-13, tagged admin  filename  generator  upload 
(1 comment)

bash alias to cleanup

Just add this to your ~/.bashrc

    alias sfcleanup='symfony propel-build-model && symfony propel-build-sql && symfony propel-insert-sql && php batch/load_data.php && symfony cc'

Next time you want to do a cleanup just type sfcleanup at the project dir.

by Roberto Carvajal on 2006-08-24, tagged bash  cli 
(1 comment)