Skip to content

Instantly share code, notes, and snippets.

@havvg
Created January 31, 2012 22:10
Show Gist options
  • Save havvg/1713360 to your computer and use it in GitHub Desktop.
Save havvg/1713360 to your computer and use it in GitHub Desktop.
symfony 1.4 custom route using multiple propel models
<?php
/**
* The route for matching search result URLs.
*
* Available parameters within the route:
* * product_category The slug of a product category.
* * vendor The slug of a vendor.
* * search The search string on a products name.
*
* @author Toni Uebernickel <[email protected]>
*/
class SearchResultRoute extends sfRequestRoute
{
/**
* @var ProductCategory
*/
protected $category;
/**
* @var Vendor
*/
protected $vendor;
/**
* @var array
*/
protected $products;
/**
* @var string
*/
protected $productName;
public function matchesUrl($url, $context = array())
{
if (false === $params = parent::matchesUrl($url, $context))
{
return false;
}
if (null === $this->category = ProductCategoryQuery::create()->findOneBySlug($params['product_category']))
{
return false;
}
if (null === $this->vendor = VendorQuery::create()->findOneBySlug($params['vendor']))
{
return false;
}
$this->productName = $params['product_name'];
$this->products = ProductQuery::create()
->filterByProductCategory($this->category)
->filterByVendor($this->vendor)
->filterByName('%'.trim(str_replace('*', '%', $params['product_name']), '% ').'%', Criteria::LIKE)
->find()
;
return $params;
}
public function generate($params, $context = array(), $absolute = false)
{
// unescape, if escaped
foreach ($params as $key => $param)
{
if (method_exists($param, 'getRawValue'))
{
$params[$key] = $param->getRawValue();
}
}
if ($params['product_category'] instanceof ProductCategory)
{
$params['product_category'] = $params['product_category']->getSlug();
}
if ($params['vendor'] instanceof Vendor)
{
$params['vendor'] = $params['vendor']->getSlug();
}
$params['product_name'] = (string) $params['product_name'];
return parent::generate($params, $context, $absolute);
}
public function getProductCategory()
{
return $this->category;
}
public function getVendor()
{
return $this->vendor;
}
public function getProducts()
{
return $this->products;
}
public function getProductName()
{
return $this->productName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment