【问题标题】:Symfony relations between tables表之间的 Symfony 关系
【发布时间】:2011-04-13 08:18:08
【问题描述】:

我使用 symfony 1.4.10 我有下一个情况: 用户可以创建“广告”,他可以创建一个“公司”。我的架构部分:

Ads:
  actAs:
    Timestampable: ~ 
    Sluggable:
      unique: true
      fields: [title]
      canUpdate: false
  connection: doctrine
  tableName: ads
  columns:
    ad_id: { type: integer(4), primary: true, autoincrement: true, notnull: true  }
    user_id: {type: int(4) }
    country: {type: string(255), notnull: true  }
    category_id: { type: int(4), notnull: true  }
    company: {type: string(255), notnull: true  }
    address: {type: string(255), notnull: true  }
    contact_person: {type: string(255), notnull: true  }
    phone:         { type: string(50), notnull: true }
    fax:         { type: string(50) }
    email: {type: string(255), notnull: true}
    show_in_profile: { type: boolean,notnull: true , default: false }
    title: {type: string(255), notnull: true  }
    content: {type: string(), notnull: true  }
    company: {type: string(255), notnull: true  }
    AGB: { type: boolean,notnull: true , default: false }
    active:          { type: boolean,notnull: true , default: 0 }
    expires_at:   { type: timestamp, notnull: true }
  relations:
    Owner:       { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser }
    Bookmark:    {                    local: ad_id, foreign: user_id, class: sfGuardUser, refClass: Bookmarks }
    Categories:  { onDelete: CASCADE, local: ad_id, foreign: category_id }
    Images:      {                    local: ad_id, foreign: ad_id, type: many, class: Images }

Companies:
  actAs:
    Timestampable: ~
    Sluggable:
      unique: true
      fields: [company]
      canUpdate: false
  connection: doctrine
  tableName: companies
  columns:
    company_id: { type: integer(4), primary: true, notnull: true, autoincrement: true }
    user_id:  {type: int(4)  }
    category_id:  {type: int(4), notnull: true  }
    company: {type: string(255), notnull: true  }
    address: {type: string(255), notnull: true  }
    contact_person: {type: string(255), notnull: true  }
    phone:         { type: string(50), notnull: true }
    fax:         { type: string(50) }
    email: {type: string(255), notnull: true}
    url:  { type: string(50) }
    about: {type: string(), notnull: true}
    country: {type: string(255), notnull: true}
    active:          { type: boolean, default: 0 }
    has_company:        { type: boolean, default: 1 }
  relations:
    Owner:       { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser }
    Images_companies:      {                    local: company_id, foreign: company_id, type: many, class: Images_companies }
    Categories:  { onDelete: CASCADE, local: company_id, foreign: category_id }

sfGuardUserProfile:
  connection: doctrine
  tableName: sf_guard_user_profile
  columns:
    id:            { type: integer(4), primary: true, autoincrement: true }
    user_id:       { type: integer(4), notnull: true }
    salutation:    { type: string(10), notnull: true }
    first_name:    { type: string(30), notnull: true }
    last_name:     { type: string(30), notnull: true }
    country:       { type: string(255), notnull: true }
    postcode:      { type: string(10) , notnull: true }
    city:          { type: string(255), notnull: true }
    address:       { type: string()   , notnull: true }
    phone:         { type: string(50) }
    email:         { type: string(255), notnull: true }
    validate:      { type: string(17) }
    banned:        { type: boolean, default: 0 }
    payed_until:   { type: datetime, notnull: true}
  relations:
    User:
      class: sfGuardUser
      local: user_id
      foreign: id
      type: one
      foreignType: one
      foreignAlias: Profile
      onDelete: CASCADE

所以我需要在用户的每个广告中放置该用户公司的链接。所以我不知道该怎么做...... 部分路由:

ads:
  url: /:sf_culture/account/ads/:action/*
  param: { module: ads }
  requirements:
    sf_culture: (?:de|en|ru)
companies:
  url: /:sf_culture/account/company/:action/*
  param: { module: companies }
  requirements:
    sf_culture: (?:de|en|ru)

我认为可以在每个广告中形成与公司的链接,但我不知道如何获得我需要的公司 id。可能添加“广告”和“公司”之间的关系?也许还有另一种方法可以链接到公司吗? p.s 对不起我的英语不好

【问题讨论】:

    标签: model-view-controller symfony1 symfony-1.4


    【解决方案1】:

    这应该很容易! 转到表单,并将其添加到 configure() 方法

    public function configure() {
            parent::configure();
            unset(
                    $this['company_id'],
            );
    }
    

    当表单被保存时,在动作中处理这个:

    if ($this->form->isValid()) {               
        $user_id = // get user_id of authenticated user
        $user = // get user obj
        $company_id = $user->Companies->id;
        $temp_ad = $this->form->getObject();
        $ad->company_id = $company_id ;
                    $this->form->save();
    }
    

    另外,在您的 Ads 模型中,Company 的关系 foreign_id 应该只是 id

    【讨论】:

      【解决方案2】:

      在公司模型中,将unique index 放在user_id 上。这样一个用户只能创建一个公司 此外,在公司模型中,

      Owner:       { onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser, foreignAlias: Company }
      

      现在在代码中,您可以随时通过$user->Company 获取用户的主要公司

      在广告模型中创建对公司的引用

      Company:       { onDelete: CASCADE, local: company_id, foreign: id, class: Company, foreignAlias: Ads }
      

      现在您可以通过$company->Ads 获取公司的所有广告 或者对于用户$user->Company->Ads

      【讨论】:

      • 谢谢!我在广告模式:Companies: { onDelete: CASCADE, local: company_id, foreign: company_id, class: Companies, foreignAlias: Ads }和公司模式Owner:{ onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser, foreignAlias: Companies }中建立关系,当用户填写新的表格时,他可以选择所有公司,而我不能,该用户只能选择他的公司(我是菜鸟(
      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多