Symfony admin filter with connection

I have a heading table that has import_profile_id . import_profile has a bank_id .

On my header list page in my admins, I would like to add the ability to filter by bank_id . However, since heading does not have bank_id - it needs to go through import_profile to get this - I can’t just add the bank_id field and expect it to work.

Can someone explain how to do this? The closest I found is this post , but I don’t think it really affects my problem.

+4
source share
1 answer

This can be done using virtual columns , just like the message you found. A virtual column is a way to add new filtering criteria using the auto-generated filter provided by symfony. It works as follows:

1 - go to the admin module .yml generator and add the name of the virtual column that will create and add

 <!-- apps/backend/modules/module_name/config/generator.yml --> filter: [virtual_column_name, and, other, filter, columns] 

2 - In your lib / filter / {TableName} FormFilter.class.php (I think in your case there should be HeadingFormFilter), you should define this virtual column in the configure () / p> method

  public function configure() { //Type of widget (could be sfWidgetFormChoice with bank names) $this->widgetSchema['virtual_column_name'] = new sfWidgetFormInputText(array( 'label' => 'Virtual Column Label' )); //Type of validator for filter $this->validatorSchema['virtual_column_name'] = new sfValidatorPass(array ('required' => false)); } 

3 - override getFields () of this class to define it in the filter and set the filter function

 public function getFields() { $fields = parent::getFields(); //the right 'virtual_column_name' is the method to filter $fields['virtual_column_name'] = 'virtual_column_name'; return $fields; } 

4 - Finally, you must define a filter method. This method should be called after the template to add ... ColumnQuery , in our case there should be addVirtualColumnNameColumnQuery (and not the choice of a happy name: P), therefore

 public function addVirtualColumnNameColumnQuery($query, $field, $value) { //add your filter query! //for example in your case $rootAlias = $query->getRootAlias(); $query->innerJoin($rootAlias . '.ImportProfile ip') ->andWhere('ip.BankId = ?', $value); //remember to return the $query! return $query; } 

Done! You can find out the filter by bank_id.

+16
source

Source: https://habr.com/ru/post/1345575/


All Articles