0

I use symfony 1.4.10 I have next situation: User can create "Ads" and he can create one "company". My part of schema:

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

So I need in each Ads of user put link to company of this user.So I do not know how to do it... Part of routing:

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)

I think that it is possible in each ad to form a link to a company, but I do not know how I can get the id of company what I need.probably add relations between "Ads" and "Company"?Maybe there is another way to make a link to company? p.s Sorry for ma bad English

denys281
  • 2,004
  • 2
  • 19
  • 38

2 Answers2

1

In the companies model, put a unique index on user_id. That way a user can create only one company Also, in companies model,

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

Now in code, you can always do $user->Company to get the user's primary company

Create a reference to Company in the Ads model

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

Now you can get all Ads for a company as $company->Ads or for a user $user->Company->Ads

Prasad
  • 1,822
  • 3
  • 23
  • 40
  • Thank you!I make relations in Ads model :`Companies: { onDelete: CASCADE, local: company_id, foreign: company_id, class: Companies, foreignAlias: Ads }`and in companies model `Owner:{ onDelete: CASCADE, local: user_id, foreign: id, class: sfGuardUser, foreignAlias: Companies }`,and when user fill the form of new, he can choice of all companies, and I can not make,that user can choice only his company(I am noob( – denys281 Apr 13 '11 at 15:12
1

that should be easy! go to the form, and add this to the configure() method

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

And when the form is saved, handle this in the action:

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

Also, in your Ads model, the relation for Companies, foreign_id should just be id

Prasad
  • 1,822
  • 3
  • 23
  • 40