Ejemplo n.º 1
0
 /**
  * Multiple creation
  */
 protected function createMany()
 {
     if ($this->checkAccess) {
         call_user_func($this->checkAccess, $this->id);
     }
     $items = Yii::$app->getRequest()->getBodyParam($this->manyProperty);
     if (!is_array($items)) {
         throw new BadRequestHttpException("{$this->manyProperty} must be array");
     }
     if (count($items) > $this->manyLimit) {
         throw new BadRequestHttpException("Request Entity Too Large", 413);
     }
     /* @var $model \yii\db\ActiveRecord */
     $preparedModel = $this->prepareModel(['scenario' => $this->scenario]);
     $request = Yii::$app->getRequest();
     $reload = $request->get('reload');
     $collection = new MultistatusCollection();
     foreach ($items as $one) {
         $model = clone $preparedModel;
         $model->load($one, '');
         if ($model->save()) {
             if ($reload) {
                 $modelClass = $this->modelClass;
                 $model = $modelClass::findOne($model->primaryKey);
             }
         } elseif (!$model->hasErrors()) {
             $e = new ServerErrorHttpException('Failed to create the object for unknown reason.');
             $collection->exception($e);
             continue;
         }
         $collection->inserted($model);
     }
     return $collection;
 }