Showing posts with label Symfony. Show all posts
Showing posts with label Symfony. Show all posts

Wednesday, 7 August 2013

How to take backup of log files in symfony 1.4?

Hi All,

Execute following command on your project root folder:

php symfony log:rotate frontend prod --period=1 --history=5


In above command,
period 1 means:
    frontend_prod.log contains 1 day log data and very next day backup of same file will be saved under log/history folder and also empty it.
history 5 means:
    Under log/history folder, backup of 5 days will be stored only.


Cheers!!!

Friday, 12 July 2013

How to send mail from shell scirpt?


Hi All,

First of all we will create a sh file and write script see example below:

vim notificationFailedCron.sh

Write down the following code in the above file:

#!/bin/bash
dt=`date +%F`
echo $dt
passwd="pwd"
count=`mysql -uipay_read -p$passwd -h localhost -D database_name -B --skip-column-names -e "select count(*) from tablename where last_execution_status = 'notexecuted' AND date(created_at) = '$dt';"`
if [ $count -ge 5 ]; then

echo $count;
mail   -s "$count Payment notifications are getting failed."  abc@abc.com  <<< 'There are $count notifications are pending due to cron job failure'
fi


Now setting cronjob which will run every minute:

* * * * * /path/to/file/ipay4me_cronJob/notificationFailedCron.sh /usr/bin/php Asia/Calcutta >/dev/null 2>&1



Cheers!

Thursday, 4 July 2013

How to create cron in symfony?


Hi All,

First of all create task using the below command:

 
php symfony generate:task [your task] 



for example:

php symfony generate:task notificationFailed

After executing above command you will see that following file has been created.

Creating "/var/www/archive/ipay4me_zen/lib/task /notificationFailedTask.class.php" task file


Now,

change the following variable in above created file:

$this->namespace        = 'project';
$this->name             = '[name-for-your-task]';
$this->briefDescription = '[some short explanation of what your task does]';


You will see another method called execute() given below:

protected function execute($arguments = array(), $options = array())
{
 // initialize the database connection
 $databaseManager = new sfDatabaseManager($this->configuration);
 $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

 // add your code here

 echo "I just did nothing at all, but at least I did it successfully!\n\n";
}


Let’s give it a test run. Open your command line and enter

php symfony project:[your task]


if everything is fine then you will see "I just did nothing at all, but at least I did it successfully!" otherwise correct error and run again.

If you see the expected output you’re then ready to go to the next step.


Note: if you want to access your app.yml variable here then you have to write following line:

$this->createConfiguration('frontend', 'prod')

or

$this->createConfiguration('frontend', 'dev')

Cron Setup:

* * * * * cd [YOUR SF APP DIR] && /usr/bin/symfony project:[YOUR TASK] >>[YOUR SF APP DIR]/log/crontab.log
e.g,

* * * * * cd /var/www/archive/ipay4me_new && /usr/bin/php symfony project:notificationFailed >>/var/www/archive/ipay4me_new/log/crontab.log


Cheers!!!

Friday, 19 October 2012

How to use commit and rollback transactions using symfony?

Hi All,

$con = Doctrine_Manager::connection();
try {
    $con->beginTransaction();
    // execute all queries...
    $con->commit();
   
}catch (Exception $e) {
   
        $con->rollback();       
        // error message will come here...
   

}

Cheers!

Wednesday, 11 July 2012

How to get Relative Path and Absolute path from url_for in symfony?

Hi All,

Suppose you have following url:
http://localhost/archive/web/frontend_dev.php/supportTool/searchByWireTransfer

Now

<?php  echo url_for('supportTool/wireTransferDetails'); ?>
The above code will provide following output:
/archive/web/frontend_dev.php/supportTool/searchByWireTransfer

<?php  echo url_for('supportTool/wireTransferDetails', true); ?>
The above code will provide following output:
http://localhost/archive/web/frontend_dev.php/supportTool/searchByWireTransfer

Cheers!

Monday, 19 December 2011

How to send array from action to template in symfony?


Suppose we have testArray given following:

$this->testArray = array('ashwani', 'bablu','cintu');
$myArray = $sf_data->getRaw('testArray');
print_r($myArray);

Output will be in template file:

Array
(
    [0] => 'ashwani'
    [1] => 'bablu'
    [3] => 'chintu'
)


Cheers!

Monday, 3 October 2011

How to use symfony inbuild functions in non-symfony class in lib folder?

Dear,

$sfUser = sfContext::getInstance()->getUser();
$sfRequest = sfContext::getInstance()->getRequest();
$sfController = sfContext::getInstance()->getController();

$sfUser->setAttribute('abc','yes');
$sfRequest->setMethod();
$sfController->redirct('cart/list');


Cheers!

Friday, 15 July 2011

How to get all form error in symfony?

Hi Guys,

1. First Method:

Add following line in the template:

foreach($form->getWidgetSchema()->getPositions() as $widgetName)
{
  echo $widgetName." == ".$form[$widgetName]->renderError();
  echo "<br />";
}

Using above script, you will find something like this:

first_name == Please enter First Name.
last_name == Please enter Last Name.
address1 == Please enter Address 1.

2. Second Method:

 public function getAllErrors()
  {
       $err = array();
       foreach ($this as $form_field){
         if ($form_field->hasError()){
           $err_obj = $form_field->getError();
           if ($$err_obj instanceof sfValidatorErrorSchema){
             foreach ($err_obj->getErrors() as $err){
               $err[$form_field->getName()] = $err->getMessage();
             }
           }else{
             $err[$form_field->getName()] = $err_obj->getMessage();
           }
         }
       }
       // global err
       foreach ($this->getGlobalErrors() as $validator_err){
         $err[] = $validator_err->getMessage();
       }
       return $err;
  }

put above function in your form class. and call this function in your action.class.php as given below:

$this->form->getAllErrors();

Using above method, you will get something like this:

Array
(
    [first_name] => Please enter First Name.
    [last_name] => Please enter Last Name.
    [address1] => Please enter Address 1.
    [town] => Please enter Town.
    [country] => Please select Country
    [email] => Please enter E-Mail address
    [phone] => Please enter Phone Number
    [address_proof] => Please upload credit card statement.
    [user_id_proof] => Please upload identity proof.
    [card_Num] => Please enter Card Number
    [card_holder] => Please enter Card Holder name.
    [transaction_type] => Please select transaction type.
)


Cheers!

Tuesday, 10 May 2011

How to create error pages in symfony?

Hi Guys,

Symfony provides following error pages:
1.) indexSuccess.php
2.) error404Success.php
3.) loginSuccess.php
4.) secureSuccess.php
5.) disabledSuccess.php
6.) unavailableSuccess.php

The above files you can find on your project. Below is the location of files;

 /var/www/project-folder/lib/vendor/symfony/lib/controller/

in controller folder, you will find a "default" module.

so all success files are located in default/templates/

Either change files here OR copy "default" module and paste it into your apps/frontend/modules/


There are two another pages which also need to be customized. you will find these default pages in the symfony_data_dir/web/errors/ directory:

    1) error500.php: Page called when an internal server error occurs.
    2) unavailable.php: Page called when a user requests a page while the cache is being cleared.

To customize these pages, create error500.php and unavailable.php pages in your application's web/errors/ directory.  Symfony will use these instead of its own.



Cheers!

Saturday, 30 April 2011

How to setup symfony project?

Hey Fox,

Download symfony project.
create folder let suppose symfony_test i.e, mkdir symfony_test
Go into symfony_test and create lib folder i.e, c:\>symfony_test> mkdir lib
Go into lib folder and create vendor folder ie., c:\>symfony_test\lib\> mkdir vendor

Now,

extract symfony project into vendor folder.

Further, open command prompt and type the following
c:\> cd wamp\www\symfony_test
c:\wamp\www\symfony_test> php lib\vendor\symfony\data\bin\symfony generate:project jobeet
c:\wamp\www\symfony_test> php symfony configure:database "mysql:host=localhost;dbname=jobeet" root root
c:\wamp\www\symfony_test> symfony generate:app --escaping-strategy=on --csrf-secret=UniqueSecret frontend


OR



Now, create the frontend application by running the generate:app task:
$ php symfony generate:app frontend




create model, form and filter on the basis of schema.yml and then

I am going to create a module "job" on the basis of model "JobeetJob"

c:\wamp\www\symfony_test> php symfony doctrine:generate-module --with-show --non-verbose-templates frontend job JobeetJob


Cheers!



How to run symfony on dos prompt?

Hey,

To run symfony in windows machine, you just set the Environment Vairables if not set for php.

Following are the steps to set Environment Variables on window machine:

Right click on MY Computer
Go to properties
Choose the Advanced tab
Click Environment Variables
Now you can edit the environment variables of the system.
In this window, you will see User Variables and System Variables.
If you change in User Varibale then changes will be applicable for your login only.
If you change in System Variable then changes will be applicable for all users.
So,
find "PATH" variable in the list of system variables.
Select it and press edit button given below.

Let Suppose, this example using wamp and wamp installed in D: drive.

now,

append the following line in variable value fields:
D:\wamp\bin\php\php5.2.8\

this folder conatains php.exe file.

now press OK and save the configuration.

Futher, the most important point. Restart your machine.


Note the ; character. It is used to separate directories in the variables, so be sure it is present.

Again, D:\path\to\php is where your php.exe is located.

Cheers!

Tuesday, 26 April 2011

How to put error message below the field in symfony form?

Hey,

Got to /lib/form/doctrine/BaseFormDoctrine.class.php
adn put following line in public function setup().
$row_format   = "<tr>\n<th>%label%</th>\n<td>%help%%field%%error%%hidden_fields%</td>\n</tr>\n";
$this->getWidgetSchema()->getFormFormatter()->setRowFormat($row_format);

see example below:


## /lib/form/doctrine/BaseFormDoctrine.class.php
<?php

/**
 * Project form base class.
 *
 * @package    passportServices
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormBaseTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
abstract class BaseFormDoctrine extends sfFormDoctrine
{
  public function setup()
  {     
    $row_format   = "<tr>\n<th>%label%</th>\n<td>%help%%field%%error%%hidden_fields%</td>\n</tr>\n";
    $this->getWidgetSchema()->getFormFormatter()->setRowFormat($row_format);     
  }
}

Cheers!

Tuesday, 6 July 2010

How to get connection from doctrine to run sql query in Symfony?

Hey folks,

$db = Doctrine_Manager::getInstance()->getCurrentConnection();
$sql = "UPDATE admin_info SET admin_category='".$permissions."' WHERE username = '".$username."'";
$result = $db->execute($sql);       
return $result;

Cheers!

Friday, 11 June 2010

How to create query parameter in symfony?

There are two ways to make query parameters:

First Method:


$params = array(
    'name'    => $request->getParameter('name'),
    'email'   => $request->getParameter('email'),
    'message' => $request->getParameter('message'),
  );

http_build_query($params);

Second Method:

$this->form = new ContactForm();
http_build_query($this->form->getValues());


Cheers!

Create Module from command line in symfony?

First go to the your project folder and then type 

$ php symfony generate:module frontend contact

it will generate all the folder related to contact module.

Cheers!

How to clear Simfony Chache?

First go to the project foleder and then type the following command on command line i.e., Terminal

$ php symfoy cc

and then press enter.

This will clear all the cache of your project.

Thanks

Cheers!

How to GET, POST or REQUEST parameters in symfony?

suppose, randomToken variable coming from the last page either GET or POST.

Using the following command, you can fetch that variable:

$rondomToken = $this->getRequestParameter('randomToken');

Cheers!

How to set Cookie in symfony?

// cookie getter
$string = $this->getRequest()->getCookie('mycookie');
 
// cookie setter
$this->getResponse()->setCookie('mycookie', $value);
 
// cookie setter with options
$this->getResponse()->setCookie('mycookie', $value, $expire, $path, $domain, $secure);
 
If you want to manipulate cookies outside of an action, you will need to access the Request and Answer objects without shortcut: 
 
$request  = sfContext::getInstance()->getRequest();
$response = sfContext::getInstance()->getResponse();
 
 
Cheers! 

How to set, get and remove session variables in symfony?

## Create Session Variables...
$this->getUser()->setAttribute('nickname', 'ashwani');


## Retrieve Session Variables...
$this->nickname = $this->getUser()->getAttribute('nickname');
   
## Check if session is exist return 1 else blank value...
$hasNickname = $this->getUser()->hasAttribute('nickname');

## Remove Session Variable...
$this->getUser()->getAttributeHolder()->remove('nickname');

Cheers!