private function importDA($data) { foreach ($data as $d) { $id = $d[0]; $model = DatasetAuthor::model()->findByPk($d[0]); if (!$model) { $model = new DatasetAuthor(); $model->id = $d[0]; } $model->dataset_id = $d[1]; $model->author_id = $d[2]; $model->rank = $d[3]; if (!$model->save()) { echo "Failed to save " . $model->id; } } }
public function actionAddAuthor() { if (isset($_POST['dataset_id']) && isset($_POST['Author'])) { $attrs = $_POST['Author']; if (!($attrs['first_name'] and $attrs['last_name'])) { Util::returnJSON(array("success" => false, "message" => Yii::t("app", "You must input first namd and last name."))); } $da = DatasetAuthor::model()->findByAttributes(array('dataset_id' => $_POST['dataset_id']), array('order' => 'rank desc')); if (!$da) { $rank = 1; } else { $rank = intval($da->rank) + 1; } $author = new Author(); $author->first_name = $attrs['first_name']; $author->surname = $attrs['last_name']; if (isset($attrs['middle_name'])) { $author->middle_name = $attrs['middle_name']; } if (isset($attrs['orcid'])) { $author->orcid = $attrs['orcid']; } if ($author->save()) { $da = new DatasetAuthor(); $da->dataset_id = $_POST['dataset_id']; $da->author_id = $author->id; $da->rank = $rank; if ($da->save()) { Util::returnJSON(array("success" => true)); } } Util::returnJSON(array("success" => false, "message" => Yii::t("app", "Save Error."))); } }
private function setAuthorList($dataset_id, $authors) { $temp = explode(";", $authors); foreach ($temp as $key => $value) { $value = trim($value); if (strlen($value) > 0) { $author = Author::model()->find("name=?", array($value)); if (!$author) { // Author not found $author = new Author(); $author->name = $value; $author->orcid = "orcid"; $author->rank = 0; $author->save(true); } $dataset_author = new DatasetAuthor(); $dataset_author->dataset_id = $dataset_id; $dataset_author->author_id = $author->id; $dataset_author->save(true); } } }