SelectManagers to v7.x

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    SelectManagers to v7.x

    Hello,

    As SelectManagers is depreciate, who can help me to adapt to v7x ?
    certainly https://docs.espocrm.com/development...a/select-defs/. but no sample :S

    Patient entity have many2many Users with a custom column "role".
    i use "assignedsUsers" because i need out-of-box assignedUser.




    PHP Code:
    
    <?php
    
    namespace Espo\Custom\SelectManagers;
    
    class Patient extends \Espo\Core\SelectManagers\Base
    {
    
      protected function boolFilterOnlyUsers(&$result)
      {
        $wherePart = null;
        if ($this->getSeed()->hasRelation('assignedsUsers')) {
           $this->setDistinct(true, $result);
           $this->addLeftJoin(['assignedsUsers', 'assignedsUsersOnlyMyFilter'], $result);
           $wherePart = [
              'assignedsUsersOnlyMyFilter.id' => $this->getUser()->id
          ];
       }
    
       return $wherePart;
     }
    }
    Last edited by item; 01-02-2022, 10:15 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • dimyy
    Active Community Member
    • Jun 2018
    • 569

    #2
    May be core filters be the sample

    Comment

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #3
      Hello item,

      Here is how I managed to replace the deprecated SelectManagers class for our ServiceTicket entity with the new Select Builder mechanism:

      Step 1:Create custom "Select" classes for each type of filter previously defined in the SelectManager:
      BEFORE
      application/Espo/Modules/PropertyManagement/SelectManagers/ServiceTicket.php
      PHP Code:
      namespace Espo\Modules\PropertyManagement\SelectManagers;
      
      class ServiceTicket extends \Espo\Core\SelectManagers\Base
      {
          protected function boolFilterOpen(&$result)
          {
              $this->filterOpen($result);
          }
      
          protected function filterOpen(&$result)
          {
              $result['whereClause'][] = array('status!=' => ['Completed', 'Canceled', 'Deferred', 'Rejected']);
          }
      } 
      
      AFTER
      application/Espo/Modules/PropertyManagement/Classes/Select/ServiceTicket/PrimaryFilters/Open.php
      PHP Code:
      namespace Espo\Modules\PropertyManagement\Classes\Select\ServiceTicket\PrimaryFilters;
      
      use Espo\ORM\Query\SelectBuilder;
      use Espo\ORM\Query\Part\Condition as Cond;
      
      use Espo\Core\Select\Primary\Filter;
      
      class Open implements Filter
      {
          public function apply(SelectBuilder $queryBuilder): void
          {
              $notOpenList = ['Completed', 'Canceled', 'Deferred', 'Rejected'];
      
              $queryBuilder->where(
                  Cond::notIn(
                      Cond::column('status'),
                      $notOpenList
                  )
              );
          }
      } 
      
      application/Espo/Modules/PropertyManagement/Classes/Select/ServiceTicket/BoolFilters/Open.php
      PHP Code:
      namespace Espo\Modules\PropertyManagement\Classes\Select\Ser viceTicket\BoolFilters;
      
      use Espo\Core\Select\Bool\Filter;
      
      use Espo\ORM\Query\SelectBuilder;
      use Espo\ORM\Query\Part\Where\OrGroupBuilder;
      use Espo\ORM\Query\Part\Condition as Cond;
      
      class Open implements Filter
      {
          public function apply(SelectBuilder $queryBuilder, OrGroupBuilder $orGroupBuilder): void
          {
              $notOpenList = ['Completed', 'Canceled', 'Deferred', 'Rejected'];
      
              $orGroupBuilder->add(
                  Cond::notIn(
                      Cond::column('status'),
                      $notOpenList
                  )
              );
          }
      } 
      
      Step 2:Create custom metadata script to have Espo invoke the new classes to apply filters instead of using the deprecated SelectManager definitions:
      application/Espo/Modules/PropertyManagement/Respurces/metadata/selectDefs/ServiceTicket.json
      Code:
      {
          "primaryFilterClassNameMap": {
              "open": "Espo\\Modules\\PropertyManagement\\Classes\\Selec t\\ServiceTicket\\PrimaryFilters\\Open"
          },
          "boolFilterClassNameMap": {
              "open": "Espo\\Modules\\PropertyManagement\\Classes\\Selec t\\ServiceTicket\\BoolFilters\\Open"
          }
      }
      Please note that I did this using the core "Task" entity as a guide and I only replaced one type of filter ("Open"), for other filters you might want to check these scripts: https://github.com/espocrm/espocrm/t...Classes/Select

      Best Regards
      Last edited by telecastg; 01-08-2022, 07:45 PM.

      Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #4
        Hello,
        Thanks telecastg , dimyy, I understand.. but sometime i have hard to find where find sample.


        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment


        • telecastg
          telecastg commented
          Editing a comment
          I agree, it would be a great improvement if the documentation would include code examples of actual usage of the new features that most users could understand.

          I am going to include a link t this posting at the "adapting to Espo 7 post" so other users can find it.

        • item
          item commented
          Editing a comment
          Hello telecastg,
          i try to do sample... with my github
          don't kill me, certainly many error but..

          i think my cisco call-to-click is best of the best
      • yuri
        Member
        • Mar 2014
        • 8453

        #5
        If you use IDE, by using 'Find Usages' feature on an interface (in the documentation it's stated what interface classes should implement) to find implementation examples and a factory class (looking into a factory class can give information about all ways the class can be defined in metadata.
        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...