Announcement

Collapse
No announcement yet.

Extend Filter interface to append conditions to where clause

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extend Filter interface to append conditions to where clause

    Hi Devs,

    I have three custom filters which filter a lot of the same fields where only one field is different. I wanted to put all the common fields in one class so they don't have to be duplicated and then extend it to handle the one field that is different for the three filters.

    I have the common class which implements the interface:

    Code:
    <?php
    namespace Espo\Custom\Select\BatchNumber;
    
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    use Espo\ORM\Query\Part\Condition as Cond;
    
    class IsNotClosed implements Filter
    {
        // https://forum.espocrm.com/forum/developer-help/75890-how-to-own-primary-filter-espocrm-7-0-x
       public function apply(SelectBuilder $queryBuilder): void
        {
            $GLOBALS['log']->debug('in is batch not closed filter custom fiter - regular  ');
            $queryBuilder->where([
                'closed'                                       => false,
                'pcbBatch!='                               => null,
                'lensBatch!='                              => null,
                'numberInBatch!='                      => null,
                'buttonsPcbBatch!='                    => null,
                'largePolarizingFilterBatch!='      => null,
                'frontOpticsAsmBatch!='             => null,
                'ledRingPcbBatch!='                    => null,
                'liquidLensCameraAssembly!='   => null,
                'controllerPCB!='                          => null,
                'usbCableAssembly!='                  => null,
            ]);
        }
    }
    ​
    And then I've extended that to handle the last field (I have three of these classes where 'type' equals a different string in each case) eg:

    Code:
    <?php
    
    namespace Espo\Custom\Select\BatchNumber;
    
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    use Espo\ORM\Query\Part\Condition as Cond;
    use Espo\Custom\Select\BatchNumber\IsNotClosed;
    
    class IsNotClosedLite extends IsNotClosed implements Filter
    {
        // https://forum.espocrm.com/forum/developer-help/75890-how-to-own-primary-filter-espocrm-7-0-x
       public function apply(SelectBuilder $queryBuilder): void
        {
            $GLOBALS['log']->debug('in is batch not closed filter custom fiter - lite');
            $queryBuilder->where([
                'type'       => 'Lite Micro' // <-- field that has a different value for each filter
            ]);
        }
    }
    ​
    What I want to end up with in practice is all those fields together in one where clause like this:

    Code:
            $queryBuilder->where([
                'closed'                                       => false,
                'pcbBatch!='                               => null,
                'lensBatch!='                              => null,
                'numberInBatch!='                      => null,
                'buttonsPcbBatch!='                    => null,
                'largePolarizingFilterBatch!='      => null,
                'frontOpticsAsmBatch!='             => null,
                'ledRingPcbBatch!='                    => null,
                'liquidLensCameraAssembly!='   => null,
                'controllerPCB!='                          => null,
                'usbCableAssembly!='                  => null,
                'type'                                             => 'Lite Micro'
            ]);​
    It's not working though. At first I thought the where clause in the IsNotClosedLite class was clobbering the where clause in the IsNotClosed class, however it appears that the IsNotClosed class is not running at all because I can't see my debug logging there. I'm not sure if I'm able to extend a class that implements an interface or if something else is going wrong.

    Can anyone please help? Is it possible to do this or do I need to duplicate all the fields in each of the filters?

    Cheers,
    Clare

  • #2
    Hi, It should work. Maybe you didn't define the filter in metadata, or didn't clear cache.

    Comment

    Working...
    X