Urkman
(Stefan Sturm)
August 1, 2014, 11:22am
1
Hello,
here at the documentation I find a lot of reading data and relations.
http://www.yiiframework.com/doc-2.0/guide-active-record.html
I use Pivot tables and everything works fine…
But, how can I change these hasMandy relations in my Pivot Table?
Is there a way to handle this with my Active Record Models or do I need to use SQL Statements here?
Thanks,
Urkman
evstevemd
(Stefano Mtangoo)
August 1, 2014, 1:58pm
2
what do you want to achieve?
Urkman
(Stefan Sturm)
August 1, 2014, 4:38pm
3
Let’s say I have a User in table “users” and a List of Groups in table “groups”.
My pivot table is "user_groups".
And now I got call call from my site, that I need set a new list of groups for a user…
Greetings,
Urkman
Urkman
(Stefan Sturm)
August 2, 2014, 6:04am
4
I solved it this way, but I thought there is a more ActiveRecord approach…
// first delete all current groups for the user
$connection->createCommand()->delete('user_groups', 'users_id = '.$user->id)->execute();
// now save the new groups
foreach($params['group_list'] as $lId) {
$data = [
'users_id' => $user->id,
'groups_id' => $lId,
];
$connection->createCommand()->insert('user_groups', $data)->execute();
}
evstevemd
(Stefano Mtangoo)
August 2, 2014, 9:56pm
5
Urkman:
Let’s say I have a User in table “users” and a List of Groups in table “groups”.
My pivot table is "user_groups".
And now I got call call from my site, that I need set a new list of groups for a user…
Greetings,
Urkman
So you mean single user can belong to multiple groups? And you want to update that user’s group or insert new (which one)?
If just updating user_group use its AR model, no need to think even in terms of Table.
AFAIK Pivot issues comes only when you do access data via it!