This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

softpider
softpider

I have three DB tables

  • Quizs Table
  • Questions Table
  • Options Table

Here Question has many Options, other side Quiz has many Question.

In my first part, Question has many Options. And that part working (with Relation Behavior Config), No problem. I can create Question and Option with relation.

But in second part when i make "has many" relationship with Quiz to Question and going to create Quiz and click Create Question button it show a popup box (with some row html tags text), in that box it showing a line (bellow)

Relation behavior does not contain a definition for 'options'

But options is in Question controller (view folder) Relation behavior. I mean options have relation with question and its working.

Please help me :(

pratyushpundir6424
pratyushpundir6424

I'm not sure what your models look like but the following is essentially how the relations should be defined in each model :

Assuming your plugin is Acme\QuizPlugin

Quiz Model

public $hasMany = [        
    'questions' => 'Acme\QuizPlugin\Models\Question'
];

Question Model

public $hasMany = [        
    'options' => 'Acme\QuizPlugin\Models\Option'
];

/** Assuming a question can only be part of a single quiz. Otherwise use belongsToMany */
public $belongsTo = [        
    'quiz' => 'Acme\QuizPlugin\Models\Quiz'
];

Option Model

/** Assuming an option can only be part of a single Question. Otherwise use belongsToMany */
public $belongsTo = [        
    'question' => 'Acme\QuizPlugin\Models\Question'
];

Of course you will also need to have your DB tables setup to work accordingly. Read this for how you need to do this -> https://octobercms.com/docs/database/relations

pratyushpundir6424
pratyushpundir6424

It'd be a bit easier to help if you could share your code. Are you trying to use the Relation Manager as described on this page? -> https://octobercms.com/docs/backend/relations

softpider
softpider

Yes I have the same model relations that you show in your code

PollQuiz Model:

public $hasMany = [
    'questions' => ['Softpider\Ladybug\Models\Question']
];

Question Model:

public $hasMany = [
    'options' => 'Softpider\Ladybug\Models\Option'
];

public $belongsTo = [        
    'pollquiz' => 'Softpider\Ladybug\Models\PollQuiz'
];

Option Model:

public $belongsTo = [        
    'question' => 'Softpider\Ladybug\Models\Question'
];

Here are my Controllers:

PollQuiz/Question Controller:

public $implement = [
    'Backend\Behaviors\ListController',
    'Backend\Behaviors\FormController',
    'Backend\Behaviors\RelationController'
];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $relationConfig = 'config_relation.yaml';

PollQuiz config_relation.yaml:

questions:
    label: Question
    manage:
        form: $/softpider/ladybug/models/question/fields.yaml
        list: $/softpider/ladybug/models/question/columns.yaml
    view:
        list: $/softpider/ladybug/models/question/columns.yaml
        toolbarButtons: create|delete

Question config_relation.yaml:

options:
    label: Option
    manage:
        form: $/softpider/ladybug/models/option/fields.yaml
        list: $/softpider/ladybug/models/option/columns.yaml
    view:
        list: $/softpider/ladybug/models/option/columns.yaml
        toolbarButtons: create|delete

Question has many Options. And that part working (with Relation Behavior Config), No problem. I can create Question and Option with relation.

But when i create Quiz and click Create Question button it show a popup box and showing that

Link of error screen capture

Last updated

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.