Back to Cumulus Core Support

roulendz
roulendz
UserModel::extend(function ($model) {
    $model->belongsToMany['clusters'] = [
        Cluster::class,
        'table' => 'initbiz_cumuluscore_cluster_user',
        'order' => 'full_name',
        'key' => 'cluster_id',
        'otherKey' => 'user_id'
    ];
});
'key' => 'cluster_id',
'otherKey' => 'user_id'

should be

'key' => 'user_id',
'otherKey' => 'cluster_id'

I have no idea how to fix it with migrations :D I just changed keys straight in the init.php! And all is good because the database is fresh just user 1 and cluster 1 :D

Last updated

roulendz
roulendz

InIT.biz said:

You wrote the same before and after. You mean that it should be:

'key' => 'user_id, 'otherKey' => 'cluster_id'

?

Yes,

Cou can check in DB and see,

I had a problem with

public function addUserToCluster(int $userId, string $clusterSlug)
    {
        $this->refreshCurrentCluster($clusterSlug);
        if ($this->currentCluster) {
            $user = $this->userRepository->find($userId);

            Event::fire('initbiz.cumuluscore.addUserToCluster', [$user, $this->currentCluster]);

            $user->clusters()->add($this->currentCluster);
        }
    }

The code does not check if the user is already attached to cluster and throws an error.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-1' for key 'PRIMARY' (SQL: insert into `initbiz_cumuluscore_cluster_user` (`cluster_id`, `user_id`) values (1, 2))" on line 664 of F:\laragon\www\modelbook\vendor\laravel\framework\src\Illuminate\Database\Connection.php

Can you give the quick fix to this? This is how I saw that USER and CLUSTER ID is in wrong places in DB

inIT
inIT

You are right. Oh my, what a shame :)

Give me a second. Working on it.

inIT
inIT

I do not know how it happened and lived for so long.

I swapped column names in migration and fixed the relation in code (version 1.3.12). Hopefully it will work and migrate successfully. In my PostgreSQL environment worked with no problems.

Give a feedback :)

Last updated

roulendz
roulendz

InIT.biz said:

I do not know how it happened and lived for so long.

I swapped column names in migration and fixed the relation in code (version 1.3.12). Hopefully it will work and migrate successfully. In my PostgreSQL environment worked with no problems.

Give a feedback :)

Ok I will try

roulendz
roulendz

roulendz said:

InIT.biz said:

I do not know how it happened and lived for so long.

I swapped column names in migration and fixed the relation in code (version 1.3.12). Hopefully it will work and migrate successfully. In my PostgreSQL environment worked with no problems.

Give a feedback :)

Ok I will try

Yes all worked like a charm :)

Any suggestions regarding check, if the user already attached to cluster, then do nothing.

public function addUserToCluster(int $userId, string $clusterSlug)
    {
        $this->refreshCurrentCluster($clusterSlug);
        if ($this->currentCluster) {
            $user = $this->userRepository->find($userId);

            Event::fire('initbiz.cumuluscore.addUserToCluster', [$user, $this->currentCluster]);

            $user->clusters()->add($this->currentCluster);
        }
    }

I have case, where this could happen.

inIT
inIT

If you can do this than create PR on Github please, I will definitely merge it :)

roulendz
roulendz

InIT.biz said:

If you can do this than create PR on Github please, I will definitely merge it :)

PR Done, please check.

1-8 of 8