I have two models with many-to-many relationships. I want to assign groups to a user. In my form I want to assign multiple already existing groups to a user. In the documentation I can only find info on how to create a User with many Groups. How do I accomplish this?
Phalcon version: 2.1.0
In the User model I have tried with a function like this that do not update the UserHasGroup table:
public function setMultipleGroupsById($array) {
$groups = array();
foreach($array as $id) {
$group = Model\Group::findFirstById($id);
$groups[] = $group;
}
$this->groups = $groups;
return $this;
}
User class:
class User extends \Phalcon\Mvc\Model {
public $id;
public $name;
public function initialize() {
$this->hasManyToMany("id", "Model\UserHasGroup", "user_id", "group_id", "Model\Group", "id", array('alias' => 'groups'));
}
}
Group class:
class Group extends \Phalcon\Mvc\Model {
public $id;
public $title;
public function initialize() {
$this->hasManyToMany("id", "Model\UserHasGroup", "group_id", "user_id", "Model\User", "id", array('alias' => 'users'));
}
}
UserHasGroup class:
class UserHasGroup extends \Phalcon\Mvc\Model
{
public $user_id;
public $group_id;
}