Archive for March, 2009

Using Multiple Databases in Symfony with Propel

The first thing that we need to do is properly configure the database configuration files (schema.yml & databases.yml).
——————————————————————————————————————
databases.yml

all:
propel:
class:          sfPropelDatabase
param:
dsn:          mysql://USERNAME:PASSWORD@DB_HOST_1/DB_NAME
slave:
class:          sfPropelDatabase
param:
dsn:          mysql://USERNAME:PASSWORD@DB_HOST_2/DB_NAME

——————————————————————————————————————

// get all articles from the slave database
$c = new Criteria();
$articles = ArticlePeer::doSelect($c, Propel::getConnection(‘slave’));
// save an article in the master database (explicitly setting the connection name)

$article = new Article();
$article->setTitle(‘Symfony Rocks!!!’);

$article->save(Propel::getConnection(‘propel’);

No Comments

Restricting user actions using credentials in generated admin modules symfony 1.2

The fields in the generator can take a credentials parameter into account so as to appear only to users who have the proper credential. This works for the list view and the edit view. Additionally, the generator can also hide interactions according to credentials.

# The id column is displayed only for users with the admin credential
    list:
      title:          List of Articles
      layout:         tabular
      display:        [id, =title, content, nb_comments]
      fields:
        id:           { credentials: [admin] }

## The addcomment interaction is restricted to the users with the admin credential
    list:
      title:          List of Articles
      object_actions:
        _edit:        -
        _delete:      -
        addcomment:   { credentials: [admin], name: Add a comment, action: addComment, icon: backend/addcomment.png }

1 Comment

Adding batch files symfony 1.2

Initializing symfony just takes a couple lines of PHP code. You can take advantage of all symfony features by creating a PHP file, for instance under the lib/ directory of your project, starting with the lines shown below.

Sample Batch Script, in lib/myScript.php

<?php
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
sfContext::createInstance($configuration);

// Remove the following lines if you don't use the database layer
$databaseManager = new sfDatabaseManager($configuration);
$databaseManager->loadConfiguration();

// add code here

No Comments

Changing the default web folder symfony 1.2

Let’s imagine that your shared host requires that the web folder is named www/ instead of web/, and that it doesn’t give you access to the httpd.conf file, but only to an .htaccess file in the web folder.

In a symfony project, every path to a directory is configurable. Chapter 19 will tell you more about it, but in the meantime, you can still rename the web directory to www and have the application take it into account by changing the configuration, as shown in Listing 3-2. These lines are to be added to the end of the config/ProjectConfiguration.class.php file.

Listing 3-2 – Changing the Default Directory Structure Settings, in config/ProjectConfiguration.class.php

class ProjectConfiguration extends sfProjectConfiguration
{
   public function setup()
   {
     $this->setWebDir($this->getRootDir().'/www');
   }
}

No Comments