Php Symfony一对多关系显示多种形式

Php Symfony一对多关系显示多种形式,php,symfony1,Php,Symfony1,我和Symfony之间有一个简单的问题 在本例中,我有一个User、LookingFor和LookingForNames表。用户表保存用户帐户,并与LookingFor有关系。LookingFor表保存用户和LookingForNames之间的任何关系 示例:用户“链”在LookingFor表中可能有两个用于约会和交谈的条目,它们是根据LookingFor和LookingForNames中的类型_id从LookingForNames表中查找的 我遇到的问题是当我在用户表单中嵌入查找关系时。它显示

我和Symfony之间有一个简单的问题

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

示例:用户“链”在LookingFor表中可能有两个用于约会和交谈的条目,它们是根据LookingFor和LookingForNames中的类型_id从LookingForNames表中查找的

我遇到的问题是当我在用户表单中嵌入查找关系时。它显示了两次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
class UserEditForm extends BaseUserForm
{
  public function configure()
  {
            $this->embedRelation('LookingFor');
  }
}
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
       ));
   }
}
解决方案是以复选框格式显示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

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

LookingFor Form - Instance #2
Dating - Not Checked
Talk - Checked
Friends - Not Checked
class UserEditForm extends BaseUserForm
{
  public function configure()
  {
            $this->embedRelation('LookingFor');
  }
}
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
       ));
   }
}
查找表单

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

LookingFor Form - Instance #2
Dating - Not Checked
Talk - Checked
Friends - Not Checked
class UserEditForm extends BaseUserForm
{
  public function configure()
  {
            $this->embedRelation('LookingFor');
  }
}
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
       ));
   }
}

看看sfGuard模式,因为它做的是相同的事情,但将用户与组链接起来。

这可能是您的模式存在的问题。用户不应该与LookingFor有关系,用户应该与LookingForNames有多对多关系。正在查找作为refClass的


如果不想更改模式,还可以通过手动嵌入表单来解决此问题。看看EmbedderRelation在内部做了什么。

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

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

LookingFor:
  uid:
    integer
  dating:
    integer
  friends:
    integer
  small_hats:
    integer
  ...
这样,查找甚至可以与用户一对一地进行