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

Ryan17977
Ryan17977

Is there a way to refer to the same model multiple times in relationship definitions?

The following works great:

public $belongsTo = [
    'user' => ['Backend\Models\User'],
    'choices' => ['Valaas\Test\Models\Choice', 'key'=>'choice_1_id'],
];

But I'm trying to use the same model to store multiple "choices", like the following:

public $belongsTo = [
    'user' => ['Backend\Models\User'],
    'choice1' => ['Valaas\Test\Models\Choice', 'key'=>'choice_1_id'],
    'choice2' => ['Valaas\Test\Models\Choice', 'key'=>'choice_2_id'],
];

I changed both the fields.yaml and $relationConfg yaml file to reflect the "choice1" and "choice2" keys, but I get this error: "Relation behavior does not contain a definition for 'choices'." Apparently the form field key must be named "choices" after the "Choice" Model? I'm probably going about this the wrong way, but any help would be greatly appreciated.

Ryan17977
Ryan17977

Got it to work eventually using the following code (it's not pretty but whatever):

public $belongsTo = [
    'user' => ['Backend\Models\User'],
    'choices' => ['Valaas\Test\Models\Choice',  'key'=>'choice_1_id'],
    'choices2' => ['Valaas\Test\Models\Choice', 'key'=>'choice_2_id'],
];

If someone can explain why changing "choice1" to "choices" makes the code work, I'd appreciate it.

Last updated

1-2 of 2

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