public static function loadMultiple($data, $modelClass, $models = [], $requiredData = [])
    {
        $result = ['models' => $models, 'errors' => []];
        if (!$data) {
            return $result;
        }
        if (!is_array($models)) {
            $data =  [@$models->id => $data];
            $models = [@$models->id => $models];
            $result = ['models' => $models, 'errors' => []];
        }
        $lastIndex = count($models);
        foreach ($data as $id => $attributes) {
            $modelSearch = array_filter($models, function($v) use($id){return $v['id'] == $id;});
            /** @var Model $model */
            $model = reset($modelSearch) ? : new $modelClass();
            foreach ($requiredData as $k => $v) {
                $model->$k = $v;
            }
            $model->load($attributes, '');
            $index = $modelSearch ? key($modelSearch) : $lastIndex++;
            $result['models'][$index] = $model;
            $model->isAttributeSafe('scenario');
            if (!$model->validate()) {
                $result['errors'][$index] = $model->errors;
            }
        }

        return $result;
    }
public static function loadMultiple($data, $modelClass, $models = [], $requiredData = [])
    {
        $result = ['models' => $models, 'errors' => []];
        if (!$data) {
            return $result;
        }
        if (!is_array($models)) {
            $data =  [@$models->id => $data];
            $models = [@$models->id => $models];
            $result = ['models' => $models, 'errors' => []];
        }
        $lastIndex = count($models);
        foreach ($data as $id => $attributes) {
            $modelSearch = array_filter($models, function($v) use($id){return $v['id'] == $id;});
            /** @var Model $model */
            $model = reset($modelSearch) ? : new $modelClass();
            foreach ($requiredData as $k => $v) {
                $model->$k = $v;
            }
            $model->load($attributes, '');
            $index = $modelSearch ? key($modelSearch) : $lastIndex++;
            $result['models'][$index] = $model;
            $model->isAttributeSafe('scenario');
            if (!$model->validate()) {
                $result['errors'][$index] = $model->errors;
            }
        }

        return $result;
    }
public static function loadMultiple ( $ data , $ modelClass , $ models = [ ] , $ requiredData = [ ] ) { $ result = [ 'models' => $ models , 'errors' => [ ] ] ; if ( ! $ data ) { return $ result ; } if ( ! is_array ( $ models ) ) { $ data = [ @ $ models -> id => $ data ] ; $ models = [ @ $ models -> id => $ models ] ; $ result = [ 'models' => $ models , 'errors' => [ ] ] ; } $ lastIndex = count ( $ models ) ; foreach ( $ data as $ id => $ attributes ) { $ modelSearch = array_filter ( $ models , function ( $ v ) use ( $ id ) { return $ v [ 'id' ] == $ id ; } ) ; /** @var Model $model */ $ model = reset ( $ modelSearch ) ? : new $ modelClass ( ) ; foreach ( $ requiredData as $ k => $ v ) { $ model -> $ k = $ v ; } $ model -> load ( $ attributes , '' ) ; $ index = $ modelSearch ? key ( $ modelSearch ) : $ lastIndex ++ ; $ result [ 'models' ] [ $ index ] = $ model ; $ model -> isAttributeSafe ( 'scenario' ) ; if ( ! $ model -> validate ( ) ) { $ result [ 'errors' ] [ $ index ] = $ model -> errors ; } } return $ result ; }
Inserts data into multiple models.
@param $data
@param $modelClass
@param array $models
@param array $requiredData
@return array
Inserts data into multiple models .