设置多对多关系

发布于 2024-11-28 04:02:00 字数 1551 浏览 1 评论 0原文

我有:

  @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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-12-05 04:02:00
   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);
     }
  }
   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);
     }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文