2011-04-13 75 views
0

我使用symfony 1.4.10 我有下一個情況: 用戶可以創建「廣告」,他可以創建一個「公司」。我的架構的一部分:表間Symfony關係

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 

所以我需要在用戶把鏈接公司本user.So的每一個廣告,我不知道該怎麼做...路由 部分:

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對不起,對不起英文

回答

1

應該很容易! 去的形式,並且將其添加到配置()方法

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

而且,在你的廣告模式,關係公司,foreign_id應該是ID

1

在公司模型中,把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

+0

謝謝!我在Ads模型中建立關係:'公司:{onDelete:CASCADE,local:company_id,foreign:company_id,class:Companies,foreignAlias:Ads}'和公司模型'Owner:{onDelete:CASCADE,local:user_id,foreign:id,class:sfGuardUser,fore ignAlias:Companies}',當用戶填寫新的表格時,他可以選擇所有公司,而且我不能製作,該用戶只能選擇他的公司(我是noob( – denys281 2011-04-13 15:12:10