public function actionIndex() { $LoadApi = new LoadApi(); // $jsonField = array_keys(array_change_key_case($jsonData->arrayperson[0],CASE_LOWER)); $apiPerson = $LoadApi->person; // $apiPerson = [''] if (count($apiPerson) > 0) { $apiField = ArrayHelper::getValue($apiPerson, 'field'); $apiData = ArrayHelper::getValue($apiPerson, 'data'); // $apiField = ['exc1','exc2']; if (count($apiField) > 0) { $fieldList = new FieldList(); // $dbField = $fieldList->find()->select('field')->orderBy(['field' => SORT_ASC])->asArray()->all(); $dbField = $fieldList::loadFields(); // $f = $fieldList::loadFields($dbField,1); // $insertedField = 0; //Check if database is blank. if (count($dbField[0]) === 0 && count($dbField[1]) === 0) { //Insert all new json field into the database. $dbField = FieldList::saveMultipleField($apiField); } else { //Check for new field, excluded field and update existing field from excluded to new //Check for new field $new = array_diff($apiField, array_merge($dbField[0], $dbField[1])); $exclude = array_diff($dbField[0], $apiField); $renew = array_intersect($apiField, $dbField[1]); $dbField = FieldList::loadAllFields(); if (count($new) > 0) { $dbField = FieldList::saveMultipleField($new); } if (count($exclude) > 0) { $dbField = FieldList::excludeMultipleFields($exclude); } if (count($renew) > 0) { $dbField = FieldList::renewMultipleFields($renew); } } } else { //Load Data from Local Database (Not included in this version). //Below for test only $apiField = $apiPerson; $apiData = $apiPerson; } } else { //Load Data from Local Database (Not included in this version). //Below for test only $apiField = $apiPerson; $apiData = $apiPerson; } $exclude = array_filter($dbField, function ($ar) { return (int) $ar['excluded'] === 1; }); $exclude = new ArrayDataProvider(['key' => 'id', 'allModels' => $exclude]); $include = array_filter($dbField, function ($ar) { return (int) $ar['excluded'] === 0; }); $include = new ArrayDataProvider(['key' => 'id', 'allModels' => $include]); $searchModel = $dbField; return $this->render('index', ['include' => $include, 'exclude' => $exclude, 'searchModel' => $searchModel]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = FieldList::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'visibility' => $this->visibility, 'new' => $this->new, 'excluded' => $this->excluded, 'inserted_at' => $this->inserted_at]); $query->andFilterWhere(['like', 'field', $this->field])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
public static function renewMultipleFields($fields) { $time = new Expression("NOW()"); $data = 0; foreach ($fields as $value) { $record = FieldList::find()->where(['field' => $value])->one(); if ($record) { $record->excluded = 0; $record->excluded_at = null; $record->renewed_at = $time; $record->new = 0; $record->update(); $data = $data + 1; } } // return $data; return FieldList::find()->orderBy(['field' => SORT_ASC])->asArray()->all(); }
/** * Finds the FieldList model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return FieldList the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = FieldList::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }