Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Snippets by user Fabien Potencier Snippets by user Fabien Potencier

How to have common parameters for every route

Let's take an example. You want to have the user culture in front of each of your URL.

So, your routing configuration look like something like that:

page:
  url: /:culture/:page
  params: ...

article:
  url: /:culture/:year/:month/:day/:slug
  params: ...

...

This is the easy part. But now, every time you call url_for() or link_to() you have to pass the culture parameter.

For such situations, there is a better way. The routing has a special configuration parameter named sf_routing_defaults that you can set with default values:

sfConfig::set('sf_routing_defaults', array('culture' => 'en'));

And now, the culture parameter will automatically added to the parameters that you pass to the url_for() or link_to() helpers.

by Fabien Potencier on 2006-08-23, tagged i18n  routing 
(7 comments)

Create your own symfony PEAR package

Sometimes, you want to create your own symfony PEAR package from the current trunk or from a particular branch. Or perhaps you did a checkout of the trunk and did some changes.

It's very simple to create your own PEAR package from such a directory:

pake release 0.8.1 stable

and pake will create a symfony PEAR package with the version 0.8.1.

You will now be able to install this with the pear command line tool:

pear install symfony-0.8.1.tgz
by Fabien Potencier on 2006-08-23, tagged installation  package  pear 
(1 comment)

Override save() and delete() methods in the admin generator

Sometimes, you want to do something just before or just after saving or deleting an object in the admin generator. To do that, you can override the saveObject() and/or deleteObject() method in your actions.class.php file:

protected function saveCustomer($customer)
{
  // pre save hook
  $customer->setManagerId($this->getUser()->getManagerId());
 
  // call the parent save() method
  parent::saveCustomer($customer);
 
  // post save hook
}

You can also bypass the parent method if you want.

protected function deleteCustomer($customer)
{
  $customer->isDeleted(true);
 
  // save the customer object
  $this->saveCustomer($customer);
 
  // bypass the deletion by not calling the parent method
}
by Fabien Potencier on 2006-05-21, tagged admin 
(4 comments)

Restrict objects displayed in the admin generator

If you want to restrict the objects displayed in the admin generator, you can add a permanent criteria by overriding the addFiltersCriteria() method:

protected function addFiltersCriteria(&$c)
{
  $c->add(CustomerPeer::DELETED, false);
}
by Fabien Potencier on 2006-05-21, tagged admin 

Change session storage

By default, symfony stores user sessions in files.

You can store them in your database by changing your apps/APPNAME/config/factories.yml configuration file:

all:
  storage:
    class: sfMySQLSessionStorage
    param:
      database: propel
      db_table: SESSION_TABLE_NAME

There are several available session storage classes:

The API documentation lists all available configuration parameters.

by Fabien Potencier on 2006-05-21, tagged database  session 
(1 comment)

Use the model in a batch or a test

To use database connections defined in the databases.yml configuration file, you can use the following snippet:

// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
by Fabien Potencier on 2006-05-21, tagged batch  configuration  propel  test 
(2 comments)

Use transaction in the model

$con = Propel::getConnection();
try
{
  $con->begin();
 
  // do something
 
  $con->commit();
}
catch (Exception $e)
{
  $con->rollback();
  throw $e;
}
by Fabien Potencier on 2006-05-20, tagged propel  transaction 
(2 comments)

Use InnoDB with Propel

To enable InnoDB support in Propel, you can add this line at the end of your config/propel.ini configuration file:

propel.mysql.tableType = InnoDB
by Fabien Potencier on 2006-05-20, tagged mysql  propel