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

lucas.sanner54070
lucas.sanner54070

This is the one to one relationship of my Membership plugin:
In my Member model:

 ...   

 public $belongsTo = [
     'user' => ['RainLab\User\Models\User']
 ];

 public static function getFromUser($user)
 {
     if ($user->member) {
         return $user->member;
     }

     $member = new static;
     $member->user = $user;
     $member->save();
     $user->member = $member;        
     return $member;
 }   

 ...

in my Plugin.php file:

 public function boot()
 {   
     \RainLab\User\Models\User::extend(function($model) {
         $model->hasOne['member'] = ['AuthorName\Membership\Models\Member'];
     });

     Event::listen('rainlab.user.register', function($user, $data) {
         \AuthorName\Membership\Models\Member::getFromUser($user);
     });
 }

Now if I use the following function, I get the email of the user corresponding to the user_id column, which is the expected result.

public function getEmailAttribute()
{
     return $this->user->email;
}

But, if I want to dispay the email into a field in field.yaml:

 user:
     label: Email
     type: relation
     select: email

I get a dropdown list filled with the emails of all the users.
Why ?
How can I limit the result to a single value ?

mjauvin
mjauvin

try:

user[email] :
     label: Email
     type: text
lucas.sanner54070
lucas.sanner54070

That was it. Thanks !
Just a quick one while we're at it. In the list columns I need to access attributes of a nested relationship called Profile.

$member->user->profile->some_attribute; 

I tried a lot of combinations but none of them has worked so far. Ex:

 first_name:
     label: First name
     relation: user[profile]
     select: first_name
     searchable: true

Is it possible to access a nested relationship attribute in a column and make it searchable ?

mjauvin
mjauvin

I frankly don't know, but I would try a partial field for this, it might give you more control.

lucas.sanner54070
lucas.sanner54070

Indeed, a partial is a good approach. I also found out the following:

 user[profile][first_name]:
     label: First name
     type: text
     searchable: true

Unfortunately both approaches are not searchable and the solutions I've found so far sound tedious and complicated.
So I think the best way to go is to make the Profile model a belongsTo relation.

 public $belongsTo = [
     'user' => ['RainLab\User\Models\User'],
     'profile' => ['AuthorName\Profile\Models\Profile']
 ];      

In that way, the profile attributes of the user are easily accessible.

1-5 of 5

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