Reputation: 13
I have:
@method Users setMail() Sets the current record's "mail" value
@method Users setUsersGroups() Sets the current record's "UsersGroups" collection
public function save(Doctrine_Connection $conn = null) {
if($this->isNew()) {
$this->setMail('[email protected]')); // ok
$this->setUsersGroups(2); // doesn't work - error Couldn't call Doctrine_Core::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.
}
parent::save($conn);
}
Table UsersGroups:
user_id
group_id
this is many to many relations.
how can i set users for groups?
example schema:
Users:
columns:
id:
type: integer(4)
autoincrement: true
primary: true
mail:
type: string(255)
password:
type: string(255)
attributes:
export: all
validate: true
Group:
tableName: group_table
columns:
id:
type: integer(4)
autoincrement: true
primary: true
name:
type: string(255)
relations:
Users:
foreignAlias: Groups
class: User
refClass: GroupUser
UsersGroups:
columns:
group_id:
type: integer(4)
primary: true
user_id:
type: integer(4)
primary: true
relations:
Group:
foreignAlias: UsersGroups
Users:
foreignAlias: UsersGroups
Upvotes: 1
Views: 761
Reputation: 52372
public function save(Doctrine_Connection $conn = null) {
if($this->isNew()) {
$this->setMail('[email protected]')); // ok
parent::save($conn);
$ug = new UsersGroups();
$ug->setUserId($this->getId());
$ug->setGroupId(2);
$ug->save();
} else {
parent::save($conn);
}
}
Upvotes: 1