Announcement

Collapse
No announcement yet.

Custom primary filter for multi-enum

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

  • Custom primary filter for multi-enum

    I have created a custom primary filter but it is not working, i don't know how to get values for a multi-enum field, below code doesn't work. anyone knows how to get value for multi-enum field using the SelectBuilder :

    Code:
    <?php
    
    namespace Espo\Custom\Classes\Select\Account\PrimaryFilters;
    
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    
    class Clients implements Filter
    {
    public function apply(SelectBuilder $queryBuilder): void
    {
    $queryBuilder->where([
    'companyType' => ['Client', 'Introducer'],
    ]);
    }
    }
    the companyType is a multi-enum field so i am trying get the companyType values both Client and Introducer.

    Thanks
    Rabii
    Web Dev

  • #2
    Hello rabii

    please use this:
    PHP Code:
    $queryBuilder->where([
    'status=' => ['Active''Closed']
    ]); 
    You simply forgot about sign = next to field name

    Comment


    • esforim
      esforim commented
      Editing a comment
      I don't see the difference between yours code (symbol) and his.

      His "=" sign is same position as yours, everything else seem same also.

  • #3
    esforim he posted
    'companyType' =>

    And mine example:
    'status=' =>


    Comment


    • esforim
      esforim commented
      Editing a comment
      I mean that just fieldname isn't it? Otherwise the 'syntax' is all good like yours... My coding skill is next to the trash can so I can't tell where the error is.

      UPDATE: OK, I totally didn't see that double = sign.
      Last edited by esforim; 04-05-2022, 09:42 AM.

  • #4
    Hi,
    your sample is for enum field, not for multi-enum
    see on GitHub closed issue where Yuri have post solution

    Is your feature request related to a problem? Please describe. I have a multi-enum field called (companyType) on account entity and i created a primary filter (Client Introducers) for my account en...

    Comment


    • emillod
      emillod commented
      Editing a comment
      you right. I'm sorry. I didn't noticed that.

  • #5
    item thanks for your reply i already tried your solution before posting here and it didn't work because that solution would only work for enum and in my case i am using multi-enum which treated differently, big thanks to yuri who helped me sort it out, there are two ways to achieve this see both code below:

    Code:
    <?php
    
    namespace Espo\Custom\Classes\Select\Account\PrimaryFilters;
    
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    use Espo\Core\Select\Where\ConverterFactory;
    use Espo\Entities\User;
    
    class PartnerClients implements Filter
    {
    private $converterFactory;
    private $user;
    public function __construct(ConverterFactory $converterFactory, User $user){
    $this->converterFactory = $converterFactory;
    $this->user = $user;
    }
    public function apply(SelectBuilder $queryBuilder): void
    {
    $converter = $this->converterFactory->create('Account', $this->user);
    $whereClause = $converter->convert(
    $queryBuilder,
    (new \Espo\Core\Select\Where\ItemBuilder)
    ->setAttribute('accountType')
    ->setType('arrayAllOf')
    ->setValue(['Client', 'Introducer'])
    ->build()
    );
    $queryBuilder->where($whereClause);
    }
    }
    Second way to achieve this below (both ways works) - second way allow to give more specific conditions e.g only accountType (Introducer):

    Code:
    <?php
    
    namespace Espo\Custom\Classes\Select\Account\PrimaryFilters;
    
    use Espo\Core\Select\Primary\Filter;
    use Espo\ORM\Query\SelectBuilder;
    use Espo\ORM\Query\Part\Condition as Cond;
    
    class OnlyActiveIntroducers implements Filter
    {
    public function apply(SelectBuilder $queryBuilder): void
    {
    $introducer = SelectBuilder::create()
    ->from('ArrayValue')
    ->select('entityId')
    ->where([
    'value' => 'Introducer',
    'attribute' => 'accountType',
    'entityType' => 'Account',
    'deleted' => false,
    ])
    ->build();
    
    $notClient = SelectBuilder::create()
    ->from('ArrayValue')
    ->select('entityId')
    ->where([
    'value' => 'Client',
    'attribute' => 'accountType',
    'entityType' => 'Account',
    'deleted' => false,
    ])
    ->build();
    
    $notAccountant = SelectBuilder::create()
    ->from('ArrayValue')
    ->select('entityId')
    ->where([
    'value' => 'Accountant',
    'attribute' => 'accountType',
    'entityType' => 'Account',
    'deleted' => false,
    ])
    ->build();
    
    $queryBuilder
    ->where(
    Cond::and(
    Cond::in(Cond::column('id'), $introducer),
    Cond::notIn(Cond::column('id'), $notClient),
    Cond::notIn(Cond::column('id'), $notAccountant),
    Cond::in(Cond::column('state'), ['Active']),
    )
    );
    }
    }
    I hope this help someone.
    Thank you guys for your support, i love this community.
    Rabii
    Web Dev

    Comment


    • item
      item commented
      Editing a comment
      Yes, it will help many

  • #6
    rabii How does this filter fit into the entire process? What calls it? Is it automatically used or is there more code in another file? Also, what does it do?

    I'm wondering if this is a special namespace: Espo\Custom\Classes\Select\Account\PrimaryFilters

    Comment


    • rabii
      rabii commented
      Editing a comment
      Hi @bandtank

      The filters allow you to have primary filter on list view of certain entity. it is powerful way to filter data based on conditions also you can add more user restrictions if you wish to. using advanced pack you can build filter reports but i prefer to use these custom filters as i can use them on dashlets as well. you can find the whole process of creating custom filters on this blog post here https://devcrm.it/custom-filters

      i hope you find it helpful if you need it in future.

  • #7
    Hi bandtank,

    https://docs.espocrm.com/development...erclassnamemap

    in V6? or V7 Yuri have do so

    Comment


    • #8
      Originally posted by item View Post
      Hi bandtank,

      https://docs.espocrm.com/development...erclassnamemap

      in V6? or V7 Yuri have do so
      I do not know what to do with the information at that link. I found it, but how is it implemented? Which files? The information is helpful, but only if you already know what to do. A complete guide would be better and I think it would only be a few sentences to explain the details.

      Comment


      • #9
        first Hi, (with my poor english)



        in metadata you declare wich and where you select class is

        then in path of rabii have posted, you implement ...

        and for language file.. i don't know but easy to find..

        I use on github "search/find" .. and try to replicate

        Comment


        • #10
          Originally posted by item View Post
          first Hi, (with my poor english)

          https://github.com/espocrm/espocrm/b...s/Account.json

          in metadata you declare wich and where you select class is

          then in path of rabii have posted, you implement ...

          and for language file.. i don't know but easy to find..

          I use on github "search/find" .. and try to replicate
          Thanks. That helps a lot, actually. I don't see a need for primary filters after looking at the code in the repository because the advanced pack allows reports to be used as filters. It's good to know how to do it, though.

          Comment

          Working...
          X