Announcement

Collapse
No announcement yet.

SelectBuilder for multi-enum fields Error while adding construct.

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

  • SelectBuilder for multi-enum fields Error while adding construct.

    Hello,
    can someone help with my issue.

    I need to get data from DB based on arrayAnyOf query type.
    I found the solutions here https://github.com/espocrm/espocrm/issues/2268
    here is one that I've chose:

    yurikuzn commented on Apr 1, 2022 •​
    Using the select-builder framework.

    Inject Espo\Core\Select\SelectBuilderFactory into your class.
    PHP Code:

    $query 
    $this->selectBuilderFactory
    ->create()
    ->
    from('EntityType')
    ->
    withWhere(
    (new 
    \Espo\Core\Select\Where\ItemBuilder)
    ->
    setAttribute('companyType')
    ->
    setType('arrayAllOf')
    ->
    setValue(['Client''Introducer'])
    ->
    build()
    )
    ->
    build();

    $collection $this->entityManager
    ->getRDBRepository('EntityType')
    ->
    clone($query)
    ->
    find();​ 

    There are also several examples from other users too.

    But my issue starts right after I'm adding the constuctor:


    PHP Code:
    <?php

    namespace Espo\Custom\Controllers;

    use 
    Espo\ORM\Entity;
    use 
    Espo\Core\Select\SelectBuilderFactory;
    use 
    Espo\Core\Api\Request;

    class 
    StockManager extends \Espo\Core\Templates\Controllers\BasePlus  {

     private 
    SelectBuilderFactory $SelectBuilderFactory;

     public function 
    __construct(SelectBuilderFactory $SelectBuilderFactory) {
          
    $this->selectBuilderFactory $selectBuilderFactory;
    }

    The ERROR returns this
    ERROR: Slim Application Error Type: Error Code: 0 Message: Typed property Espo\Core\Controllers\RecordBase::$searchParamsFet cher must not be accessed before initialization File: /var/www/html/application/Espo/Core/Controllers/RecordBase.php Line: 291


    Thank you



  • #2
    Hi,

    It's not good to write business logic in controllers. Your controller should call some other classes (that class will require select-builder-factory via controller). This class we call a 'service'. It's a plain class, not extended from anything.

    This question is more PHP language related rather then Espo framework. As the controller is extended, you are trying to change the constructor. You shouldn't do that. You can either use injectableFactory to instantiate your service class or create another not extended controller.

    PHP Code:
    $this->injectableFactory->create(Service::class)->process( ... ); 

    I recommend reading Dependency Injection article from the docs and using an IDE like PHPStorm.

    Comment

    Working...
    X