$connection = sfContext::getInstance()->getDatabaseConnection('propel');
If you want the Propel connection from a model class, always use the following to allow your model classes to be used outside a symfony context:
$connection = Propel::getConnection();
A full example of custom SQL taken from the askeet tutorial
public function executeAutocomplete() { $tags = array(); $con = sfContext::getInstance()->getDatabaseConnection('propel'); $query = ' SELECT DISTINCT %s AS tag FROM %s WHERE %s = ? AND %s LIKE ? ORDER BY %s '; $query = sprintf($query, QuestionTagPeer::TAG, QuestionTagPeer::TABLE_NAME, QuestionTagPeer::USER_ID, QuestionTagPeer::TAG, QuestionTagPeer::TAG ); $stmt = $con->prepareStatement($query); $stmt->setInt(1, $this->getUser()->getSubscriberId()); $stmt->setString(2, $this->getRequestParameter('tag').'%'); $stmt->setLimit(10); $rs = $stmt->executeQuery(); while ($rs->next()) { $tags[] = $rs->getString('tag'); } $this->tags = $tags;
Comments on this snippet
If you want the Propel connection from a model class, always use the following to allow your model classes to be used outside a symfony context:
A full example of custom SQL taken from the askeet tutorial