yii\di\ServiceLocator::setComponents PHP Method

setComponents() public method

This is the bulk version of ServiceLocator::set. The parameter should be an array whose keys are component IDs and values the corresponding component definitions. For more details on how to specify component IDs and definitions, please refer to ServiceLocator::set. If a component definition with the same ID already exists, it will be overwritten. The following is an example for registering two component definitions: php [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'sqlite:path/to/file.db', ], 'cache' => [ 'class' => 'yii\caching\DbCache', 'db' => 'db', ], ]
public setComponents ( array $components )
$components array component definitions or instances
    public function setComponents($components)
    {
        foreach ($components as $id => $component) {
            $this->set($id, $component);
        }
    }

Usage Example

コード例 #1
0
 /**
  * @param array $requestParams
  * @param array $config
  */
 public function __construct($requestParams = [], $config = [])
 {
     parent::__construct($config);
     $this->queryBuilder = \Yii::createObject(QueryBuilder::class, [null]);
     $this->locator = new ServiceLocator();
     $this->locator->setComponents($this->attributeHandlers());
     $query = $this->getQueryInstance();
     $query->query = new Bool();
     $query->filter = new \opus\elastic\elastica\filter\Bool();
     $query->limit = ArrayHelper::remove($requestParams, 'limit');
     $query->offset = ArrayHelper::remove($requestParams, 'offset');
     $query->orderBy = ArrayHelper::remove($requestParams, 'sort');
     $this->requestParams = $requestParams;
     $this->setAttributes();
 }