Blog
DIY tips: Inheriting Views
2007-07-16 22:39:46 by Martynas Jusevičius
Sometimes you may come up with a View that does nothing else than some additional filtering compared to a more general View.
For example, imagine a product catalog with nested categories. One way to create Views for it is to have a top-level ProductListView showing all the categories and all the products in them, and a View for each of the nested categories, e. g. ValveListView, CompressorListView etc. Then, this situation allows some nice code reuse via inheritance of the Views. The specific category ListViews inherit from the ProductListView and only do the additional filtering of the products, making sure only valves or compressors or whatever appear in that category.
One way to implement the filtering is making Propel's Criteria object (used for filtering the database results) a protected field of the ProductListView. The XSLT template can be overridden in the constructor as well, if necessary. It would probably also make sense to move the inherited View into a sub-folder of ProductListView, but then include paths have to be modified accordingly.
Here is how the final inherited ValveListView might look like:
class ValveListView extends ProductListView
{
public function __construct(Resource $resource = null)
{
parent::__construct($resource);
$this->template->load(ROOT_DIR."view/views/productList/valveList/ValveList.xsl");
}
public function display(Request $request, Response $response)
{
$this->criteria->add(ProductPeer::TYPE, ProductPeer::CLASSKEY_VALVEPRODUCT);
parent::display($request, $response);
}
}