Get access to data on current record from primary filter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • onepoint0
    Member
    • Jul 2023
    • 41

    Get access to data on current record from primary filter

    Hi Devs,

    I have a one to one relationship set up between a device and its internal camera. The camera is a selectable field on the device. The camera select modal has a primary filter on it created using the selectDefs/primary filter method described here. It filters out cameras which are already attached to another device eg

    PHP Code:
    <?php
    namespace Espo\Custom\Select\Camera;
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    use Espo\ORM\Query\Part\Condition as Cond;
    use Espo\ORM\Query\SelectBuilder as QueryBuilder;
    use Espo\Core\Select\Text\Filter\Data;
    
    class NotAttachedToDevice implements Filter
    {
        // https://forum.espocrm.com/forum/developer-help/75890-how-to-own-primary-filter-espocrm-7-0-x
        // public function apply(QueryBuilder $queryBuilder, Data $data): void
       public function apply(SelectBuilder $queryBuilder): void
        {
    
            $subQuery = QueryBuilder::create()
                ->select('id')
                ->from('Device','device')
                ->where([
                // if the camera is currently attached (deviceCurrCameraId is set OR production camera is set but not current - that is, it hasn't been replaced) then it can't be used
                    'OR' => [
                        [
                            'device.deviceCurrCameraId:' => 'Camera.id',
                        ],
                        [
                            'device.deviceProdCameraId:' => 'Camera.id',
                            'device.deviceCurrCameraId'  => null,
                        ]
                    ]
                ])
                ->build();
    
                $queryBuilder->where( Cond::not(Cond::exists($subQuery)));
    
        }
    }
    
    But now I have a requirement to match the type of the device (macro/micro) to the type of the camera (macro/micro). To do that I need access to the type of the device (data on the current record)... The id or the type itself would work but I can't figure out how to get access to that info. Can anyone please help?

    Cheers,
    Clare
  • yuri
    Member
    • Mar 2014
    • 8453

    #2
    Hi Clare,

    You can't do it with primary filters by design. You can utilize regular field (advanced) filters. It's possible to have them forcibly added when you select records for relation.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    Working...