This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi all, I'm trying to show a hasOne relation in the backend view. I have created the $hasOne relation variable in my model and the problem that I have now is that I'm not able to show an attribute of the referenced object in the list view of the backend. The code that I use in file columns.yaml is this one:
columns:
post_id:
label: Post
relation: post
select: title
As the documentation states. Can anyone help me, please?
Last updated
Hi, you have to use the relation name rather than the relation key as column name http://pastie.org/9843119
Last updated
Thank you for your replay. Just for avoiding confusion I renamed the relation my_post, so now in my model I have:
public $hasOne = [
'my_post' => ['RainLab\Blog\Models\Post',
'scope' => 'isPublished',
'key' => 'id']
];
According to your solution, my columns.yaml file becomes:
columns:
my_post:
label: Post
relation: my_post
select: title
And this still is not working. In the backend list view I'm not able to see the post title. Does my code implements correctly what you stated? Or I'm wrong somewhere? Thanks
Did you try without any scope any select ? in that case you should have the json object of the my_post in the column.
Did you check on Database if you're relation is there, just to check it's not a data issue ? It's really weird because if you put a column which is not a relation or a actual model column you would have an SQL telling your column does not exists. You're in development environment ?
I have been trying to do something similar and have been unable to get it working as well. When I use the select: option I get nothing displaying in my list, when I leave it off I get a full text representation of the array for that records relation table. Would be nice to figure out how to get it to show only one field like I want.
Finally I solved my problem, now the referenced object is displayed in my table! Special thanks to gilloub5404. First of all I was using a wrong declaration for the relation in columns.yaml and secondly the one-to-one relation was not decleared correctly. I left here the solution if some one needs it. If you have a model A(id, model_b_id, ...) that references model B(id, some_text) via model_b_id and not its id the $hasOne variable should be declared in this way:
public $hasOne = [
'my_relation' => [
'Namespace\B',
'key' => 'id', // key of the B class (B.id)
'otherKey' => 'model_b_id' // column of the A model that references B (A.model_b_id)
]
];
If in your list view you want to show B.some_text you need to declare your column in columns.yaml of the A model as follows:
columns:
my_relation:
label: Label
relation: my_relation
select: some_text
I'm hoping this can help somebody.
Last updated
For anyone that was using a relation
form field to display a HasOne relationship, I've just fixed a really insidious bug that would have probably made you tear your hair out: https://github.com/octobercms/library/commit/e502f3371e5f852989f4cccbcfdd10d67b54f49a
1-10 of 10