Code snippets for symfony 1.x


Refine Tags

Snippets tagged "filter admin"

Set create default value to filter defined value for a specific field

This is a very usefull and simple tip if you want to defined default value in edit form (only on creation of a new record) with the value that the user defined in the list filter.

An exemple for a product module which define the category select field with the value of the same field in the list filter.


  public function executeEdit ()
    $filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/product/filters');
    if (!$this->getRequestParameter('product_id', 0) && isset($filters['category_id']))
      $this->product = new Product();
  protected function getProductOrCreate ($product_id = 'product_id')
    if (isset($this->product)) return $this->product;
    else return parent::getProductOrCreate($product_id);
by Sylvain PAPET on 2006-10-20, tagged admin  filter  generator 

Filtering with rows from another table

By default, the admin generator allows to filter the data with the rows from the table currently listed.

Here's how to extend this to data from other linked tables.

We'll consider the following example : a table "command" linked to a table "user". This very simple schema.xml shows the relation between these two tables :

<table name="buyer" phpName="BtqBuyer" >
  <column name="buyer_id" type="BIGINT" required="true" primaryKey="true"/>
  <column name="buyer_name" type="VARCHAR" size="255" required="true"/>
<table name="command" phpName="BtqCommand" >
  <column name="com_id" type="BIGINT" required="true" primaryKey="true"/>
  <column name="com_ref" type="VARCHAR" size="6" required="true"/>
  <column name="com_buyer_id" type="BIGINT" required="true"/>
  <foreign-key foreignTable="buyer" onDelete="" onUpdate="">
    <reference local="com_buyer_id" foreign="buyer_id"/>

In the file generator.yml, add a partial in the filters parameter to print our specific filter :

filters: [com_ref, _btq_buyer]

The source code for the partial _btq_buyer.php (located in the templates directory) is :

<?php echo input_tag('filters[buyer]', isset($filters['buyer']) ? $filters['buyer'] : '') ?>

Now we have to add our specific filter in the filter process. To do this, we extend the addFiltersCriteria from the admin generator. This is done in the file actions.class.php by adding :

protected function addFiltersCriteria (&$c)
  if (isset($this->filters['buyer']) && $this->filters['buyer'] != '')
    $c->add(BtqBuyerPeer::BUYER_NAME, strtr($this->filters['buyer'], '*', '%'), Criteria::LIKE);
    $c->addJoin(BtqBuyerPeer::BUYER_ID, BtqCommandPeer::COM_BUYER_ID);

As you can see, we've even allowed the use of wildcard in our filter. Nice ;)

by marc Hugon on 2006-06-21, tagged admin  filter  generator