2017-03-08 4 views
0

나는 각 사용자가 다음과 같은 필드 간단한 중간 테이블을 통해 친구를 가질 수 있도록 rainlab.user 플러그인을 확장하고있어 : 내가했습니다OctoberCMS : 동적 관계 설정 파일에 manage.list를 필터링하는 방법

user_id 
friend_id 
status 

이를위한 사용자의 모든 친구들이 나열되어 있습니다 Friends 탭이하는 Rainlab.User Controller 확장 :

: 여기

use RainLab\User\Models\User as FrontUser; 
use RainLab\User\Controllers\Users as UsersController; 

FrontUser::extend(function($model) { 
    $model->belongsToMany['friends']=[ 
     'RainLab\User\Models\User', 
     'table' => 'meysam_social_friends', 
     'pivot' => ['status'], 
     'key'  => 'user_id', 
     'otherKey' => 'friend_id' 
    ]; 
}); 

UsersController::extend(function($controller) { 
    if(!isset($controller->implement['Backend.Behaviors.RelationController'])) { 
     $controller->implement[] = 'Backend.Behaviors.RelationController'; 
    } 
    $controller->relationConfig = '$/meysam/social/controllers/user/config_relations.yaml'; 
}); 

UsersController::extendFormFields(function($form, $model, $context) { 
    if(!$model instanceof FrontUser or $context != 'preview'){ 
     // friends tab should not be displayed in update and create contexts 
     return; 
    } 

    $form->addTabFields([ 
     'friends' => [ 
      'label' => '', 
      'tab' => 'Friends', 
      'type' => 'partial', 
      'path' => '$/meysam/social/controllers/user/_friends.htm', 
     ] 
    ]); 
}); 

을 그리고 것은 config_relations.yaml 파일입니다 현재 선택된 사용자의 친구이다의 친구 탭에서 지금

friends: 
    label: 'new friend' 
    view: 
     list: ~/plugins/meysam/social/models/friendspivot/columns.yaml 
     toolbarButtons: add|remove 
     showSearch: true 
    manage: 
     showSearch: true 
     recordsPerPage: 10 
     list: ~/plugins/rainlab/user/models/user/columns.yaml 
    pivot: 
     form: ~/plugins/meysam/social/models/friendspivot/fields.yaml 

16,목록이 표시됩니다 내가 Add new friend 버튼을 클릭하면, 모든 사용자의 목록이 표시됩니다 (manage.list) 사용자 중 하나를 선정 할 수 있도록 새로운 친구. 내 문제는이 목록에서 현재 선택된 사용자조차도 모든 사용자가 표시되므로 사용자를 자신의 친구로 추가 할 수 있다는 것입니다. 이 목록에서 현재 사용자를 필터링하려면 어떻게합니까? 내가 scope 속성을 사용하여 생각하지만,이 범위는 동적이어야하며 현재 User Model이 범위에 user_id 전달하는 방법을 모르겠어요.

답변

2

현재 모델이 뷰에 제공된 범위 메서드에 전달되고 옵션을 첫 번째 매개 변수로 관리합니다. 나는이 사실을 반영하기 위해 문서를 갱신했다.

것은 당신에게 예를 제공하기 위해, 당신의 config_relation.yaml

friends: 
    label: 'new friend' 
    view: 
     list: ~/plugins/meysam/social/models/friendspivot/columns.yaml 
     toolbarButtons: add|remove 
     showSearch: true 
    manage: 
     showSearch: true 
     recordsPerPage: 10 
     list: ~/plugins/rainlab/user/models/user/columns.yaml 
     scope: notThis 
    pivot: 
     form: ~/plugins/meysam/social/models/friendspivot/fields.yaml 

과 같아야하고 쿼리 범위 방법 notThis은 사용자 모델에 동적으로 추가해야합니다

FrontUser::extend(function($model) { 
    $model->addDynamicMethod('scopeNotThis', function ($query, $user) { 
     return $query->where('id', '!=', $user->id); 
    }); 
});