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
{
public $incrementing = false;
protected $keyType = 'string';
public $fillable = [
'id',
'name'
];

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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