/** * For each author, add an AuthorHelper */ private function migrateAuthors() { /** @var SQLQuery $query */ $query = new SQLQuery(); $query->setSelect('Author')->setFrom('News')->setDistinct(true); $authors = $query->execute(); foreach ($authors as $author) { /** Create a new author if it doesn't exist */ if (!($authorHelper = AuthorHelper::get()->filter(array('OriginalName' => trim($author['Author'])))->first())) { /** @var AuthorHelper $authorHelper */ $authorHelper = AuthorHelper::create(); $authorHelper->OriginalName = $author['Author']; $authorHelper->write(); } $sql = "UPDATE `News` SET `AuthorHelperID` = '" . $authorHelper->ID . "' WHERE Author = '" . $author['Author'] . "'"; DB::query($sql); } }
/** * Create the author if non-existing yet, and set his/her ID to this item. */ private function setAuthorData() { $this->Author = trim($this->Author); $nameParts = explode(' ', $this->Author); foreach ($nameParts as $key => $namePart) { if ($namePart == '') { unset($nameParts[$key]); } } $this->Author = implode(' ', $nameParts); $author = AuthorHelper::get()->filter('OriginalName', trim($this->Author))->first(); if (!$author->exists()) { $author = AuthorHelper::create(); $author->OriginalName = trim($this->Author); $author->write(); } $this->AuthorHelperID = $author->ID; }