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

rajakhoury
rajakhoury

I have three Models : User, UserAccount and Store

The User belongs to a UserAccount ( One-to-Many) and a UserAccount has-many Stores ( One-To-Many).

Now I can access the stores assigned to the account using : user.account.stores

But what I need is to access the stores directly user.stores in the backend..

User Model :

public $belongsTo = [
    'account' => [
        'RainLab\User\Models\UserAccount',
        'table' => 'user_accounts',
        'key' => 'account_id',
        'otherKey' => 'id'
    ]
];

UserAccount Model :

   public $hasMany = [
        // Users
        'users' => [
            'RainLab\User\Models\User',
            'table' => 'users',
            'key' => 'account_id',
            'otherKey' => 'id',
            'order' => 'name asc',
            'delete' => true,
        ],
        'users_count' => [
            'RainLab\User\Models\User',
            'table' => 'users',
            'key' => 'account_id',
            'otherKey' => 'id',
            'count' => true
        ],

        // Stores
        'stores' => [
            'RainLab\User\Models\Store',
            'table' => 'user_stores',
            'key' => 'account_id',
            'otherKey' => 'id',
            'order' => 'name asc',
            'delete' => true,
        ],
        'stores_count' => [
            'RainLab\User\Models\Store',
            'table' => 'user_stores',
            'key' => 'account_id',
            'otherKey' => 'id',
            'count' => true
        ],
    ];

Store Model :

   public $belongsTo = [

        /** Account **/
        'account' => [
            'RainLab\User\Models\UserAccount',
            'table' => 'user_accounts',
            'key' => 'account_id',
            'otherKey' => 'id'
        ]
    ];

What I need is to be able to assign in the backend a front-end User to a specific store(s) ( Many-to-Many ). I created a pivot table users_stores - user_id | store_id and I add the relation_config in the User's controller.

User Model :

   public $belongsToMany = [
        'stores' => [
            'Rainlab\User\Models\Store',
            'table' => 'users_stores',
            'key' => 'user_id',
            'otherKey' => 'store_id',
            'order' => 'name'

        ],
    ];

And Store Model :

public $belongsToMany = [
    'users'       => [
        'RainLab\User\Models\User',
        'table' => 'users_stores',
        'key' => 'store_id',
        'otherKey' => 'user_id',
        'order' => 'name'
    ]
];

User Controller - config_relation.yaml

    # ===================================
    #  Relation Behavior Config
    # ===================================

    stores:
        label: User Assigned Stores
        view:
            list: ~/plugins/rainlab/user/models/store/columns.yaml
            toolbarButtons: add|remove|link|unlink
            showCheckboxes: true
        manage:
            list: ~/plugins/rainlab/user/models/store/columns.yaml
            form: ~/plugins/rainlab/user/models/store/fields.yaml
        emptyMessage: backend::lang.list.no_records

So far it works fine, I can assign a user to a store(s) but the problem is when you click on a User a list of all stores shows up instead I need only a list of Stores that belong to that UserAccount

I am not sure if this is the correct way of doing it, I wonder if

a) is there any method like `$belongsToManyThrough ?

b) I should use Scopes when fetching Stores but How to filter by User / UserAccount

c) The logic is wrong...

Thanks

Last updated

1-1 of 1

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