Search This Blog

Thursday, December 16, 2010

How to download files in Symfony

Its very easy to download files in symfony. Create a proper action and do something like this , i have assumes that files are stored in the database and its a pdf file. But you can assign any file by storing the file type in the database.



public function  executeDownloadFile(sfWebRequest $request) {
        $yourfileData = '' // get your file data from the database
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Content-Disposition',
        'attachment; filename='. 'myfile.pdf');
        $this->getResponse()->setContentType('application/pdf');
        $this->getResponse()->sendHttpHeaders();       
        $this->getResponse()->setContent($yourfileData);

        return sfView::NONE;

}

If you need to set http headers 

private function setHttpHeaders($size, $fileName, $contentType='application/csv') {
        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->addCacheControlHttpHeader('Cache-control', 'private');
        $this->getResponse()->setHttpHeader('Content-Description', 'File Transfer');
        $this->getResponse()->setContentType($contentType, TRUE);
        $this->getResponse()->setHttpHeader('Content-Length', (string) $size, TRUE);
        $this->getResponse()->setHttpHeader('content-transfer-encoding', 'binary', TRUE);
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=' . $fileName, TRUE);
        $this->getResponse()->sendHttpHeaders();
    }

Tuesday, December 14, 2010

Recursively deletes subversion .svn folders in ubuntu

First we find the .svn folders, then we use rm to remove those folders . There are 2 methods to do this.Type this in shell


Method -1

$ find . -name ".svn" -exec rm -rf {} \;
Method -2

$ rm -rf `find . -type d -name .svn`

Sunday, December 5, 2010

Save new record or Update a record if it exist using Doctrine object

Are getting error when you save a doctrine object saying primary key violation?
Before you save the object you must search if its already there. When you use find it uses the primary key.

public function saveFamilyDetails($form) {

$employeeFamilyDetails = Doctrine::getTable('EmployeeFamilyDetails')->find($form['txtFamilyID']);

        if(!($employeeFamilyDetails instanceof EmployeeFamilyDetails) ) {
            $employeeFamilyDetails = new EmployeeFamilyDetails();
        }

        $employeeFamilyDetails->employee_id       = $form['txtEmpID'];
        $employeeFamilyDetails->fm_name       = $form['txtFamName'];
        $employeeFamilyDetails->relationship       = $form['txtFamRelationship'];
        $employeeFamilyDetails->date_of_birth        = $form['txtFamDob'];
        $employeeFamilyDetails->address        = $form['txtFamAddress'];
        $employeeFamilyDetails->mobile_no      = $form['txtFamMobileNo'];
        $employeeFamilyDetails->residence_no    = $form['txtFamRecidenNo'];
        $employeeFamilyDetails->workcon_no   = $form['txtFamWorkConNo'] ;
        $employeeFamilyDetails->dependent      = $form['txtFamDependant'];
        $employeeFamilyDetails->nric_fin    = $form['txtFamNricFin'] ;
        $employeeFamilyDetails->passpot_no   = $form['txtFamPassport'] ;

        return $employeeFamilyDetails->save();



    }

Monday, November 15, 2010

How to make word wrap in HTML table

td{
max-width: 200px; 
word-wrap: break-word;
}

Friday, November 12, 2010

Some useful symfony commands

These are some useful symfony commands


Create a symfony module

php symfony generate:module orsss  mymodule


Create symfony schema from the database

php symfony doctrine:build-schema

build models

php symfony doctrine:build-model

insert the created sql

php symfony doctrine:insert-sql

list all the back end routes

php symfony app:routes backend

publish assets of a plugin to web directory

php symfony plugin:publish-assets

create a sfGuard user

symfony guard:create-user indrakeerthi@gmail.com admin admin poojitha jayasinghe

build everything from scratch

symfony doctrine:build --all --and-load --no-confirmation

Monday, November 8, 2010

How to Uninstall Netbeans

root@user:/usr/local/netbeans-6.9# ./uninstall.sh

Tuesday, November 2, 2010

Find the last day of the given month in PHP

function getLastDayOfMonth($year,$month) {
    $lastdayOfMonth = date('Y-m-d',strtotime('-1 second',strtotime('+1
                  month',strtotime($month.'/01/'.$year.' 00:00:00'))));

    return $lastdayOfMonth;
}



This function gets current month and year as the input returns the last day of the month.

It add one month to the current month and reduce one second from the 00:00:00 th hour of the first day so to get the last day of this month.

Wednesday, October 27, 2010

Steps to install a plugin in Symfony

Listing 17-15 - Installing a Plug-In from the Symfony Wiki
> cd myproject
> php symfony plugin-install http://plugins.symfony-project.com/pluginName
> php symfony cc


Listing 17-16 - Installing a Plug-In from a Downloaded PEAR Package
> cd myproject
> php symfony plugin-install /home/path/to/downloads/pluginName.tgz
> php symfony cc


Listing 17-17 - Installing a Plug-In from a PEAR Channel
> cd myproject
> php symfony plugin-install channelName/pluginName
> php symfony cc


Don't forget to enable the plugin in ProjectConfiguration.class.php
$this->enablePlugins('sfTaskExtraPlugin')

read more

Ubuntu command to get the System hardware configurations

Run this command as the super user to get the complete hardware configurations
>lshw

Tuesday, October 26, 2010

How to run a single PHP Unit Test

How to run a single PHP Unit Test

phpunit --filter EmpSalaryReportTest AllTests.php

// Get unit test coverage report

phpunit --coverage-html ./report BankAccountTest

phpunit --coverage-html AllTests AllTests.php