Switch to using string based primary keys

This commit is contained in:
Leonard Smith 2020-10-05 15:13:37 -05:00
parent 9eacb24857
commit 9dc6a65122
9 changed files with 44 additions and 19 deletions

View File

@ -6,7 +6,14 @@ use Illuminate\Database\Eloquent\Model;
class Book extends Model class Book extends Model
{ {
public $incrementing = false;
protected $keyType = 'string';
public $fillable = [ public $fillable = [
'id',
'name' 'name'
]; ];

View File

@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\Model;
class Chapter extends Model class Chapter extends Model
{ {
public $incrementing = false;
public $fillable = [ public $fillable = [
'id',
'name' 'name'
]; ];

View File

@ -58,13 +58,14 @@ class UlbXmlImportHandler
$document = $this->openBook($filepath); $document = $this->openBook($filepath);
$book = Book::create([ $book = Book::create([
'name' => $bookTitle, 'id' => strtolower($bookTitle),
'name' => ucfirst($bookTitle),
]); ]);
$chapterCollection = $document->getElementsByTagName('chapter'); $chapterCollection = $document->getElementsByTagName('chapter');
foreach ($chapterCollection as $chapterElem) { foreach ($chapterCollection as $chapterElem) {
$chapter = $this->importChapter($chapterElem); $chapter = $this->importChapter($chapterElem, $book->id);
$book->chapters()->save($chapter); $book->chapters()->save($chapter);
} }
@ -75,16 +76,19 @@ class UlbXmlImportHandler
* @param DOMElement $chapterElem * @param DOMElement $chapterElem
* @return Chapter * @return Chapter
*/ */
public function importChapter(DOMElement $chapterElem) : Chapter public function importChapter(DOMElement $chapterElem, string $book_id) : Chapter
{ {
$chapterName = $this->parseChapterName($chapterElem);
$chapter = Chapter::create([ $chapter = Chapter::create([
'name' => $this->parseChapterName($chapterElem), 'id' => implode('-', [$book_id, $chapterName]),
'name' => $chapterName,
]); ]);
$verseCollection = $chapterElem->getElementsByTagName('verse'); $verseCollection = $chapterElem->getElementsByTagName('verse');
foreach ($verseCollection as $verseElem) { foreach ($verseCollection as $verseElem) {
$verse = $this->importVerse($verseElem); $verse = $this->importVerse($verseElem, $chapter->id);
$chapter->verses()->save($verse); $chapter->verses()->save($verse);
} }
@ -95,10 +99,13 @@ class UlbXmlImportHandler
* @param DOMElement $verseElem * @param DOMElement $verseElem
* @return Verse * @return Verse
*/ */
public function importVerse(DOMElement $verseElem) : Verse public function importVerse(DOMElement $verseElem, string $chapter_id) : Verse
{ {
$verseNumber = $this->parseVerseNumber($verseElem);
$verse = Verse::create([ $verse = Verse::create([
'name' => $this->parseVerseNumber($verseElem), 'id' => implode('-', [$chapter_id, $verseNumber]),
'name' => $verseNumber,
'greek_text' => $verseElem->getElementsByTagName('Greek')[0]->nodeValue, 'greek_text' => $verseElem->getElementsByTagName('Greek')[0]->nodeValue,
'ulb_text' => $verseElem->getElementsByTagName('ULB')[0]->nodeValue, 'ulb_text' => $verseElem->getElementsByTagName('ULB')[0]->nodeValue,
]); ]);
@ -106,7 +113,7 @@ class UlbXmlImportHandler
$wordCollection = $verseElem->getElementsByTagName('w'); $wordCollection = $verseElem->getElementsByTagName('w');
foreach ($wordCollection as $wordElem) { foreach ($wordCollection as $wordElem) {
$word = $this->importWord($wordElem); $word = $this->importWord($wordElem, $verse->id);
$verse->words()->save($word); $verse->words()->save($word);
} }
@ -117,17 +124,19 @@ class UlbXmlImportHandler
* @param DOMElement $wordElem * @param DOMElement $wordElem
* @return Word * @return Word
*/ */
public function importWord(DOMElement $wordElem) : Word public function importWord(DOMElement $wordElem, string $verse_id) : Word
{ {
// NOTE: We have to switch thins around a bit as the incoming XML file $ognt_sort = $wordElem->getAttribute('OGNTsort');
// use lexeme for lemma and lemma for the strongs number
// NOTE: We have to switch things around a bit as the incoming XML file
// uses lexeme for lemma and lemma for the strongs number
$word = Word::create([ $word = Word::create([
'id' => implode('-', [$verse_id, $ognt_sort]),
'ulb' => $wordElem->nodeValue, 'ulb' => $wordElem->nodeValue,
'greek' => $wordElem->getAttribute('text'), 'greek' => $wordElem->getAttribute('text'),
'lemma' => $wordElem->getAttribute('lexeme'), 'lemma' => $wordElem->getAttribute('lexeme'),
'morph' => $wordElem->getAttribute('morph'), 'morph' => $wordElem->getAttribute('morph'),
'ognt_sort' => $wordElem->getAttribute('OGNTsort'), 'ognt_sort' => $ognt_sort,
'strongs_number' => $wordElem->getAttribute('lemma'), 'strongs_number' => $wordElem->getAttribute('lemma'),
]); ]);

View File

@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\Model;
class Verse extends Model class Verse extends Model
{ {
public $incrementing = false;
public $fillable = [ public $fillable = [
'id',
'name', 'name',
'ulb_text', 'ulb_text',
'greek_text', 'greek_text',

View File

@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\Model;
class Word extends Model class Word extends Model
{ {
public $incrementing = false;
public $fillable = [ public $fillable = [
'id',
'ulb', 'ulb',
'greek', 'greek',
'lemma', 'lemma',

View File

@ -14,7 +14,7 @@ class CreateBooksTable extends Migration
public function up() public function up()
{ {
Schema::create('books', function (Blueprint $table) { Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id'); $table->string('id')->primary();
$table->string('name'); $table->string('name');
$table->timestamps(); $table->timestamps();
}); });

View File

@ -14,9 +14,9 @@ class CreateChaptersTable extends Migration
public function up() public function up()
{ {
Schema::create('chapters', function (Blueprint $table) { Schema::create('chapters', function (Blueprint $table) {
$table->bigIncrements('id'); $table->string('id')->primary();
$table->string('name'); $table->string('name');
$table->bigInteger('book_id')->unsigned()->nullable(); $table->string('book_id')->nullable();
$table->timestamps(); $table->timestamps();
}); });

View File

@ -14,11 +14,11 @@ class CreateVersesTable extends Migration
public function up() public function up()
{ {
Schema::create('verses', function (Blueprint $table) { Schema::create('verses', function (Blueprint $table) {
$table->bigIncrements('id'); $table->string('id')->primary();
$table->string('name'); $table->string('name');
$table->text('ulb_text'); $table->text('ulb_text');
$table->text('greek_text'); $table->text('greek_text');
$table->bigInteger('chapter_id')->unsigned()->nullable(); $table->string('chapter_id')->nullable();
$table->timestamps(); $table->timestamps();
}); });

View File

@ -14,14 +14,14 @@ class CreateWordsTable extends Migration
public function up() public function up()
{ {
Schema::create('words', function (Blueprint $table) { Schema::create('words', function (Blueprint $table) {
$table->bigIncrements('id'); $table->string('id')->primary();
$table->string('ulb'); $table->string('ulb');
$table->string('greek'); $table->string('greek');
$table->string('lemma'); $table->string('lemma');
$table->string('morph'); $table->string('morph');
$table->bigInteger('ognt_sort'); $table->bigInteger('ognt_sort');
$table->string('strongs_number'); $table->string('strongs_number');
$table->bigInteger('verse_id')->unsigned()->nullable(); $table->string('verse_id')->nullable();
$table->timestamps(); $table->timestamps();
}); });