开发者

Symfony one-to-many embedrelation showing multiple forms

I'm having what should be a simple problem with Symfony.

In this example I have a User, LookingFor, and LookingForNames table. The user table holds the user account and has a relation with LookingFor. The LookingFor table holds any relations between a user and the LookingForNames.

Example: The user 'chain', could have two entries in the LookingFor table for dating and talk, which are looked up from the LookingForNames table based on the type_id from LookingFor and LookingForNames.

The problem I am having is when I embedRelation for LookingFor within the User form. It's showing the LookingFor form twice because the user has selected they are looking for dating and talk. It would show up more times if I have more selected for that user.

eg. of the problem

LookingFor Form - Instance #1
Dating - Checked
Talk - Not Checked
Friends - Not Checked

LookingFor Form - Instance #2
Dating - Not Checked
Talk - Checked
Friends - Not Checked

The solution would be showing the LookingFor table once in a checkbox format where the user's selection would be preselected.

eg. of the solution

LookingFor Form - Only One Instance
Dating - Checked
Talk - Checked
Friends - Not Checked

schema.yml

LookingFor:
  connection: doctrine
  tableName: looking_for
  columns:
    type_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    uid:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement:开发者_开发问答 false
  relations:
    LookingForNames:
      local: type_id
      foreign: type_id
      type: many
LookingForNames:
  connection: doctrine
  tableName: looking_for_names
  columns:
    type_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    name:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    LookingFor:
      local: type_id
      foreign: type_id
      type: many
User:
  connection: doctrine
  tableName: user
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    email:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    gender:
      type: string(6)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    age:
      type: date(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    city:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    state:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    country:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    profilepic:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      default: profileblank.jpg
      notnull: false
      autoincrement: false
  relations:
    LookingFor:
      local: id
      foreign: uid
      type: many
      foreignType: many

UserEditForm

class UserEditForm extends BaseUserForm
{
  public function configure()
  {
            $this->embedRelation('LookingFor');
  }
}

LookingForForm

class LookingForForm extends BaseLookingForForm
{
  public function configure()
  {
      $this->useFields(array('type_id'));
      $this->widgetSchema['type_id'] = new sfWidgetFormChoice(array(
          'choices' => Doctrine_Core::getTable('LookingForNames')->getFormChoiceNames(),
          'expanded' => true,
          'multiple' => true
       ));
   }
}


Is LookingForNames really necessary? It seems that you are trying to add support for creating new LookingFor categories later, e.g. Food Lovers, People With Small Hats, etc.

If you really think that you are going to be adding a lot of these LookingForNames later on then this makes sense, but if you are making a dating/social site I can't imagine that you would do this very often. If you don't need to add categories frequently, try a schema like this:

LookingFor:
  uid:
    integer
  dating:
    integer
  friends:
    integer
  small_hats:
    integer
  ...

This way a LookingFor can even be one-to-one with Users.


Take a look at the sfGuard schema as it does the same thing but links users with groups.


This is probably an issue with your schema. A User shouldn't have a relation with LookingFor, Users should have a many to many relation with LookingForNames. LookingFor as the refClass.

If you don't want to change your schema, you can also fix this by manually embedding the form. Look at what embedRelation does internally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜