Symfony一对多嵌入显示多种形式

时间:2010-07-08 01:17:08

标签: php symfony1

我对Symfony有一个简单的问题。

在这个例子中,我有一个User,LookingFor和LookingForNames表。用户表包含用户帐户并与LookingFor有关系。 LookingFor表保存用户和LookingForNames之间的任何关系。

示例:用户'链'可以在LookingFor表中有两个用于约会和谈话的条目,这些条目是根据来自LookingFor和LookingForNames的type_id从LookingForNames表中查找的。

我遇到的问题是我在User表单中嵌入了ForFor for LookingFor。它显示了两次LookingFor表单,因为用户选择了他们正在寻找约会和谈话。如果我为该用户选择了更多次,它会显示更多次。

例如。问题

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

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

解决方案是以复选框格式显示一下LookingFor表格,其中将预先选择用户的选择。

例如。解决方案

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

3 个答案:

答案 0 :(得分:1)

LookingForNames真的有必要吗?您似乎正在尝试添加对以后创建新的LookingFor类别的支持,例如食物爱好者,小帽子等人

如果你真的认为你将在以后添加很多这些LookingForNames,那么这是有道理的,但如果你正在建立一个约会/社交网站,我无法想象你会经常这样做。如果您不需要经常添加类别,请尝试这样的模式:

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

这样,LookingFor甚至可以与用户一对一。

答案 1 :(得分:0)

看看sfGuard架构,因为它做同样的事情,但将用户与群组链接。

答案 2 :(得分:0)

这可能是您的架构的问题。用户不应与LookingFor有关系,用户应与LookingForNames有多对多的关系。 LookingFor作为refClass。

如果您不想更改架构,也可以通过手动嵌入表单来解决此问题。看看embedRelation在内部做了什么。