I have a table, TableModule, with 2 foreign keys, let's say fk1 and fk2.
fk1 goes to pk1 in table Table 1
fk2 goes to pk2 in table Table 2
I created a module. let's say, Module, which uses the TableModule (the one with the fk's)
I want to create 4 filters for those fk: 2 input text and 2 dropdowns. In particular, I want to create two filters per fk. This is:
For fk1 I would get:
-InputText
-Dropdown (choice in propel)
For fk2 I would get:
-InputText
-Dropdown (choice in propel)
Of course, this would show the results of Table1 and Table2.
Now, in my config.yml I got:
...
filter:
display: [fk1, fk1TextFilter, fk2, fk2TextFilter]
...
This is: fk1 and fk2 would be filtered as dropdowns, The partials fk1TextFilter and fk2TextFilter must be customized for filtering using a text input.
Why I created those partials? Because I can't duplicate the fk in the config.yml!!
In lib/filter/table1/ModuleFormFilter
I did (notice that is in table1):
public function configure()
{
$this->setWidgets(array(
'fk1' => new sfWidgetFormPropelChoice(array('model' => 'table1', 'add_empty' => true,)),
'fk1TextFilter' => new sfWidgetFormInput(),
'fk2' => new sfWidgetFormPropelChoice(array('model' => 'table2', 'add_empty' => true,)),
'fk2TextFilter' => new sfWidgetFormInput(),
));
$this->setValidators(array(
'fk1TextFilter' => new sfValidatorPropelChoice(array('model' => 'table1', 'column' => 'id', 'required' => false)),
'fk1' => new sfValidatorPropelChoice(array('model' => 'table1', 'column' => 'id', 'required' => false)),
'fk2TextFilter' => new sfValidatorPropelChoice(array('model' => 'table2', 'column' => 'id', 'required' => false)),
'fk2' => new sfValidatorPropelChoice(array('model' => 'table2', 'column' => 'id', 'required' => false)),
));
$this->validatorSchema->setPostValidator(
new sfValidatorPropelUnique(array('model' => 'table2', 'column' => array('fk2')))
);
$this->widgetSchema->setNameFormat('model[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
}
This is: created 2 textInput and 2 dropdowns as stated earlier.
If I use -could be the inputtext or the dropdowns- just the fk this will work OK. the problem is that I can't duplicate the fk's. I can't do:
$this->setWidgets(array(
'fk1' => new sfWidgetFormPropelChoice(array('model' => 'table1', 'add_empty' => true,)),
'fk1' => new sfWidgetFormInput(),
'fk2' => new sfWidgetFormPropelChoice(array('model' => 'table2', 'add_empty' => true,)),
'fk2' => new sfWidgetFormInput(),
));
If I run the page I get:
You must define a "filterByfk1TextFilter" method in the ModelQuery class to be able to filter with the "fk1TextFilter" field.
I found some links (1, 2) but is not working for me. I don't have in the symfony docs concrete examples.
What I must created and how?
By now I have, in the same lib/filter/table1/ModuleFormFilter
:
public function getFields()
{
$fields = parent::getFields();
$fields['fk1TextFilter'] = 'fk1TextFilter';
$fields['fk2TextFilter'] = 'fk2TextFilter';
return $fields;
}
public function addModelfk1TextFilterQuery($query, $field, $value)
{
//add your filter query!
//for example in your case
$rootAlias = $query->getRootAlias();
$query = ModelQuery::create()
->filterByfk1TextFilter()
->find();
//remember to return the $query!
return $query;
}
Is not working for me. Could you help me please??
Black Magic:
There are many different ways. None worked for me, at least those in the links I posted.
Nevertheless, using: add[VirtualColumnName]ColumnCriteria allows you to customize filters.
In this case, after all the code I wrote (and changing addModelfk1TextFilterQuery()) just add:
public function addfk1TextFilterColumnCriteria($query, $field, $value)
{
//Here just put a query in propel, for ex:
$query = $query->useTableModel()
->filterByName("*$value*")
->endUse()
->find();
return $query;
}
Hoping that this will help some others!
Mini Edit: do some echoes to $field
and $values
for clarification