PHP Doctrine - YAML 语法帮助。多对多关系的默认值?

发布于 2024-08-11 23:12:56 字数 1122 浏览 6 评论 0原文

我有以下 YAML 架构用于在 Doctrine 中组织用户:

Person:
  tableName: people
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true 
    firstname:
      type: string
      notnull: true
      lastname:
      type: string
      notnull: true
User:
  inheritance:
  extends: Person
    type: column_aggregation
    keyField: type
    keyValue: 1
Userdetails:
  columns:
    person_id:
      type: integer
      primary: true
    password:
      type: string
      notnull: true
  relations:
    User:
      foreignType: one
      local: person_id
      onDelete: CASCADE
      onUpdate: CASCADE
Group:
  tableName: groups
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name:
      type: string
      notnull: true
UserGroup:
  columns:
    group_id:
      type: integer
      primary: true
      person_id:
      type: integer
      primary: true
  relations:
    User:
      foreignType: many
      local: person_id
    Group:
      foreignType: many 

基本上,任何用户都将属于一个或多个组。
有什么方法可以默认将新用户添加到特定组吗?

任何建议表示赞赏。

谢谢

I have the following YAML schema for organising users in Doctrine:

Person:
  tableName: people
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true 
    firstname:
      type: string
      notnull: true
      lastname:
      type: string
      notnull: true
User:
  inheritance:
  extends: Person
    type: column_aggregation
    keyField: type
    keyValue: 1
Userdetails:
  columns:
    person_id:
      type: integer
      primary: true
    password:
      type: string
      notnull: true
  relations:
    User:
      foreignType: one
      local: person_id
      onDelete: CASCADE
      onUpdate: CASCADE
Group:
  tableName: groups
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name:
      type: string
      notnull: true
UserGroup:
  columns:
    group_id:
      type: integer
      primary: true
      person_id:
      type: integer
      primary: true
  relations:
    User:
      foreignType: many
      local: person_id
    Group:
      foreignType: many 

Basically, any people who are users will belong to one or more groups.
Is there any way to add new users to a particular group by default?

Any advice appreciated.

Thanks

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

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

发布评论

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

评论(1

几度春秋 2024-08-18 23:12:56

我必须管理,我不再使用最新的 Doctrine 版本,但由于它的记录需要默认构造函数,我认为从用户构造函数中的数据库加载默认组应该足够了,具体

class User extends Doctrine_Record
{
    public __construct()
    {
        parent::__construct();
        $this->groups[] = Doctrine::getTable('Group')->findByName('DefaultGroup');
    }
}

$blauser = new User();
$blauser->save(); // Will save the user with the default group

取决于您可能想要的架构考虑将该初始化也放入您的控制器中。

I must admin, that I'm not in the most recent Doctrine version anymore, but since its records need a default constructor I think it should be sufficient to load the default group from the database in the users constructor

class User extends Doctrine_Record
{
    public __construct()
    {
        parent::__construct();
        $this->groups[] = Doctrine::getTable('Group')->findByName('DefaultGroup');
    }
}

$blauser = new User();
$blauser->save(); // Will save the user with the default group

Depending on your architecture you might want to consider to put that initialisation into your controller, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文