This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hei everyone
I was hoping someone would be able to help me fix this small problem I'm having. I'm trying to build a small plugin with two tables, Books and Chapters. The relation is simple; Book has many Chapters and a Chapter has one Book. The foreign key in Chapter called book_id references the id in Book. I have two different controllers on the CMS for both tables; one to add books and the other to add chapters.
The Create Books form works fine, no problem.
But when I try adding a chapter, I get a constraint failure. This seems to be because the insert query does not include the book_id, despite it appearing in the Create Chapter form. From the event log I found that the insert query looks like this:
insert into `nadja_librarian_chapters` (`ChapterName`, `ChapterNumber`, `Author`, `is_Published`, `Content`, `updated_at`, `created_at`) values (Test Chapter, 3, Test Author, 1, <p>Stuff</p>, 2015-10-12 11:45:27, 2015-10-12 11:45:27
It clearly shows all the form elements are included, except for the relations field, book_id, although it appears just as it should as a drop down list on the page.
This is what the fields.yaml looks like:
tabs:
fields:
ChapterName:
label: Chapter Name
comment: Enter a name for the chapter.
tab: Details
ChapterNumber:
label: Chapter Number
comment: Enter the chapter number.
tab: Details
Author:
label: Author
comment: Enter the author's name.
tab: Details
Book:
label: Book
type: relation
tab: Details
options:
nameColumn: book_id
is_Published:
label: Publish
type: switch
tab: Details
Content:
label: Story
type: richeditor
size: huge
tab: Details
Does anyone know why? I've been wrestling with this for a while now and I'm running out of ideas to cycle. Any clues would be appreciated.
Last updated
Sure, below are the model classes Chapter, Book, and the create_chapters_table.php. They were both originally created by artisan and I just added the extra bits I wanted. Hope that generates some ideas?
class Chapter extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'nadja_librarian_chapters';
/**
* @var array Relations
*/
public $hasOne = [
'Book' => ['Nadja\Librarian\Models\Book']
];
}
class Book extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'nadja_librarian_books';
public $hasMany = [
'Chapters' => ['Nadja\Librarian\Models\Chapter', 'key' => 'book_id']
];
}
class CreateChaptersTable extends Migration
{
public function up()
{
Schema::create('nadja_librarian_chapters', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->timestamps();
$table->integer('book_id')->unsigned();
$table->string('ChapterName');
$table->integer('ChapterNumber');
$table->text('Content');
$table->date('Published')->nullable();
$table->boolean('is_Published');
$table->string('Author');
$table->foreign('book_id')->references('id')->on('nadja_librarian_books');
});
}
}
Last updated
Okey, managed to finally solve the problem by changing the relation hasOne in the Chapter model to belongsTo. Seems I missed that belongsTo is the inverse relation to hasMany, instead of hasOne.
i want do https://ctrlv.cz/d2Hl
without plugins front-end and "tabs" fields i want all into only page for users!
Last updated
1-6 of 6