Foreign key vs displayField different by default for index() and view()

I was wondering why associated data fields are shown as their displayField and foreignKey variables in foreign index() and view() templates respectively.
I found the convention of ‘name’ being the displayField variable quite handy, but I have to edit all view() templates manually (for the hasMany “Related $tablename” table) to follow this more intuitive form of reference.

Is the difference due to some design philosophy?
My guess would be that it would be too heavy to load all belongsTo models for the Related objects?

There is a way to change the skeleton that bakes those templates (though I don’t happen to know the details).

Making your own edited version should give you the nice human-readable value you want.

Also, if you thing it’s a result of the display field being wrong for that association you can do this during your association definition:

        $this->hasMany('thing')
            ->setDisplayField('choice');

Tweaking the baking is definitely on my list. I will consider it for next time such that I won’t override manual changes for now.

And would the following work (before baking) as well? In order to follow the default syntax.

$this->hasMany('thing', [
            'foreignKey' => 'choice',	
        ]); 
...
 $this->belongsTo('thing', [
            'foreignKey' => 'choice',
            'joinType' => 'INNER',
        ]);

yes. My point was simply that you could chain the setDisplayField() call onto your normal hasMany() call.

1 Like