2011-08-03 80 views
1

我:设置多对多关系

@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); 


} 

表UsersGroups:

user_id 
    group_id 

这是多对多的关系。

我该如何设置用户群组?

示例模式:

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 
+0

什么excatly你想干什么?我们可以看到你的schema.yml吗?只是User,UserGroups和User的一部分。如果模式是正确的,我不认为你会有任何问题。 – bertzzie

+0

用户没有名为UsersGroups的属性,您可以将其设置为2.它具有相关UsersGroups对象的集合(所有那些user_id是此用户的行)......将其设置为数字2是没有意义的无论是。 –

回答

1
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); 
    } 
    }