SpencerRohan
SpencerRohan

I've created a polymorphic table -- Phones.

  • A phone has a contactable_id and contactable_type.
  • A phone can belong to a restaurant or a team_member.
  • A restaurant has many phones. A team_member has many phones.

In Restaurant and Team_Member model:

public $morphMany =  ['phones' => [ 'My\Site\Models\Phone', 'name' => 'contactable' ] ];

In Phone model:

public $morphTo = [ 'contactable' => [] ];

My question is how do I build a form for this in assignment in the Phone fields.yaml? Am I setting this up right?

Any help is super appreciated. Thanks

Last updated

apocsve
apocsve

Hi Spencer, hope your doing well,

I have the same doubt, i tried to use the following on my field.yaml

what@create:
label: 'Reviewed'
type: relation

and on my Product and CustomProduct model have this

public $morphMany = [
     'reviews' => ['Clubs\Eshop\Models\Review', 'name' => 'what']
];

and on the Review model have the following morphTo

public $morphTo = [
    'what' => []
];

with this configuration i got the following issue when try to access to the create page from the cms:

class_uses(): object or string expected
/home/vagrant/projects/clubsgalore/modules/backend/FormWidgets/Relation.php line 126

and this is the line of Relation.php

// Determine if the model uses a tree trait
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
$usesTree = count(array_intersect($treeTraits, class_uses($relationModel))) > 0;

have any one a workaround for fix this issue?

i have google it and search at stackexchange but haven't found anything. :(

DMeganoski
DMeganoski

I know modifying the base code is a little scary, but the way I have been getting past obstacles in October is to simply dump some data from the base code. Just always remember to immediately ctrl + z afterwards.

Have you tried dumping $relationModel?


dd($relationModel);

What does it output?

if it outputs null, perhaps you need to follow the code up the tree a bit. find the 'makeRelation($attribute)' function and see what's going on in there.

milos.stanic
milos.stanic

Hi guys, I have a similar problem. Any workaround for this? Did you get to create a polymorphic record finder field?

aaron.humphreys
aaron.humphreys

It is not implemented. You will need to edit the source code and/or implement things in a different way if you wish to access morphic relations in the backend.

/**
 * Returns a relation class object
 * @param string $name Relation name
 * @return string
 */    
public function makeRelation($name)
{
    $relationType = $this->getRelationType($name);
    $relation = $this->getRelationDefinition($name);

    if ($relationType == 'morphTo' || !isset($relation[0])) {
        return null;
    }

    $relationClass = $relation[0];
    return new $relationClass();
}
chris10207
chris10207

hi guys, i got the similar requirement, Any workaround for this? I am trying to conditonanly load the config field of the morph relation and it is working well. Now I am having issue to save the morph relation model

PolloZen
PolloZen

from 2020, does anybody can resolve this?

daftspunky
daftspunky

This is fixed in v2.1.8 and up

jacksun
jacksun

daftspunk said:

This is fixed in v2.1.8 and up

Sorry, would you please share how to implement a poly field exactly?

1-9 of 9