Archive for the ‘Symfony’ Category

Symfony - Creating a New Plugin

Thursday, October 29th, 2009

You need this just once:

_ sudo apt-get install php-pear
_ sudo pear channel-discover plugins.symfony-project.com

For every package:
_ Create the file package.xml and put it on the root of the plugin
_ launch:      pear package

To update the repository, use this URL:

http://svn.symfony-project.com/plugins/plugin_name

Sympony - Propel - Get Connection Details

Sunday, August 16th, 2009

/**
* Returns an associative array with the details of the database connection
*
* @param string $application the name of a valid application (i.e.: frontend) - used to get a Propel connection
* @param string $environment the environment we want to apply the deltas to
* @return array(of strings) the keys are: name, host, user, password
*/
public static function getDatabaseConnectionDetails($application, $environment)

{
$databaseConnectionDetails = array();
$configuration = ProjectConfiguration::getApplicationConfiguration($application, $environment, true);
sfContext::createInstance($configuration);
$databaseManager = new sfDatabaseManager($configuration);
$databaseConfig = sfPropelDatabase::getConfiguration();
$databaseDsn = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’dsn’];

if (preg_match(’/dbname=([^;]+);/’, $databaseDsn, $matches))
{
$databaseConnectionDetails[’name’] = $matches[1];
}
else
{
throw new sfException(sprintf(’Unable to get the database name.’));
}

if (preg_match(’/;host=(.+)/’, $databaseDsn, $matches))
{
$databaseConnectionDetails[’host’] = $matches[1];
}
else
{
throw new sfException(sprintf(’Unable to get the host for the database.’));
}

$user = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’user’];
if ($user)
{
$databaseConnectionDetails[’user’] = $user;
}
else
{
throw new sfException(sprintf(’Unable to get the username for the database.’));
}

$password = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’password’];
if ($password)
{
$databaseConnectionDetails[’password’] = $password;
}
else
{
throw new sfException(sprintf(’Unable to get the username for the database.’));
}
return $databaseConnectionDetails;
}

Symfony - Hydrated Criteria

Sunday, August 16th, 2009

public static function getHydratedCriteria()
{
$criteria = new Criteria();
$criteria->addSelectColumn(’*');
$criteria->setPrimaryTableName(self::TABLE_NAME);
$criteria->addJoin(self::COMPANY_ID, CompanyPeer::ID, Criteria::INNER_JOIN);
$criteria->addJoin(self::CITY_ID, CityPeer::ID, Criteria::INNER_JOIN);
$criteria->addJoin(self::REGION_ID, RegionPeer::ID, Criteria::INNER_JOIN);
return $criteria;
}

Lucene PHP - Important Notes

Sunday, August 16th, 2009

*** Lucene - Use integers ***
Actually the book ‘Practical Symfony’ (version jobeet-1.2-propel-en-2009-02-05) is not accurate:

1) the primary key should be defined as a Keyword
$doc->addField(Zend_Search_Lucene_Field::Keyword(’pk’, $this->getId()));
2) by default, Lucene can’t find numbers in its index [and a primary key is a number!]. Then, *before any* invocation of the find method for searching a primary key, we need:
$currentWorkingDirectory = getcwd();
chdir(sfConfig::get(’sf_root_dir’) . “/lib/vendor”);
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
chdir($currentWorkingDirectory);
*** Lucene Character Escaping ***
Lucene escapes special characters automatically so you need just to make sure that the query coming from users, doesn’t contains the character that you use to build the query internally in PHP (i.e.: double quotes):
$userQuerySector = str_replace(’”‘, ‘ ‘, $querySector);
$luceneSubqueriesArray[] = ” (category:\”$querySector\” OR title:\”$querySector\”) “;
That means you don’t need to implement any data sanitization

Symfony and Eclipse - Include Library

Sunday, August 16th, 2009

To include Symfony library: ‘Add External Source Library’ during the creation of the project

Symfony - Admin on a Subset of Rows, Extra Criteria

Sunday, August 16th, 2009

In the actions.class.php, you need to override the method buildCriteria, adding the criteria you need

Propel - Sympony - Conditions in OR in Criteria

Sunday, August 16th, 2009

Propel:
$c = new Criteria();

$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, “Leo”);

$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME,
array(”Tolstoy”, “Dostoevsky”, “Bakhtin”), Criteria::IN);

$cton1->addOr($cton2);

// add to Criteria

$c->add($cton1);

$authors = AuthorPeer::doSelect($c);

Custom SQL on Symfony

Tuesday, August 11th, 2009

$connection = Propel::getConnection();
$query = ‘SELECT MAX(%s) AS max FROM %s’;
$query = sprintf($query, SbVideoPeer::ID, SbVideoPeer::TABLE_NAME);
$statement = $connection->prepare($query);
$statement->execute();
$resultset= $statement->fetch();
$max = (int)$resultset[’max’];

If that doesn’t work, you need to add on top (i.e. you are using Symfony from the command line):
$configuration = ProjectConfiguration::getApplicationConfiguration($application, $environment, true);
sfContext::createInstance($configuration);
$databaseManager = new sfDatabaseManager($configuration);

Symfony - Get the Working Environment

Thursday, August 6th, 2009

$sf_environment = sfConfig::get(’sf_environment’);

Symfony - how to Include a Partial into an External Web Site - Symfony Integration

Wednesday, August 5th, 2009

This is very good for integrating the header of a web site powered with Symfony in a Wordpress blog. That is just an example

require_once(dirname(__FILE__).’/../config/ProjectConfigurat ion.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();

sfContext::getInstance()->getConfiguration()->loadHelpers(’Partial’);
echo get_partial(”global/header”);