修改Admin Generator的Doctrine表单布局

时间:2011-10-28 09:26:15

标签: symfony1 symfony-1.4

我需要修改Admin Generator的Doctrine Form,其中包含:

$this->embedRelation('MyRelation');

默认布局如下所示:

Screenshot 1

目标 - 每个选择项目应显示为单独行中的文本,加上价格和数量:

Screenshot 2

的schema.yml

Game:
    actAs:
      Timestampable: ~
    columns:
      id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
      game_name: { type: string(100), notnull: true }
    indexes:
      it:
  fields: game_name
  type: unique

  Campaign:
    actAs:
      Timestampable: ~
    columns:
      id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
      name: { type: string(100), notnull: true }
      is_active: { type: boolean, notnull: true, default: 0 }
      start: { type: datetime, notnull: true }
      end: { type: datetime, notnull: true }
    relations:
      CampaignMatrix: { onDelete: CASCADE, local: id, foreign: campaign_id, foreignAlias: CampaignMatrixCampaign }

  CampaignGames:
    actAs:
      Timestampable: ~
    columns:
      id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
      campaign_id: { type: integer(4), notnull: true, unsigned: true }
      game_id: { type: integer(4), notnull: true, unsigned: true }
    indexes:
      tc:
  fields: [campaign_id, game_id]
  type: unique
    relations:
      Campaign: { onDelete: CASCADE, local: campaign_id, foreign: id, foreignAlias: CampaignCampaignGames }
      Game: { onDelete: CASCADE, local: game_id, foreign: id, foreignAlias: GameCampaignGames }

  CampaignMatrix:
    actAs:
      Timestampable: ~
    columns:
      id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
      item_id: { type: integer(4), notnull: true, unsigned: true }
      campaign_id: { type: integer(4), notnull: true, unsigned: true }
      price_id: { type: integer(4), notnull: true, unsigned: true }
      quantity: { type: integer(4), notnull: true, unsigned: true }
    relations:
      Item: { onDelete: CASCADE, local: item_id, foreign: id, foreignAlias: ItemCampaignMatrix }
      Campaign: { onDelete: CASCADE, local: campaign_id, foreign: id, foreignAlias: CampaignCampaignMatrix }
      Price: { onDelete: CASCADE, local: price_id, foreign: id, foreignAlias: PriceItems }

  Price:
    columns:
      id: { type: integer(4), unsigned: true }
      currency_code: { type: string(3), notnull: true }
      price: { type: float, notnull: true }
    indexes:
      tc:
  fields: [id, currency_code]
  type: unique

  Item:
    actAs:
      Timestampable: ~
      I18n:
  fields: [name, description, image]
    columns:
      id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
      game_id: { type: integer(4), notnull: true, unsigned: true }
      product_id: { type: string(100), notnull: true }
      price_id: { type: integer(4), notnull: true, unsigned: true }
      quantity: { type: integer(4), notnull: true, unsigned: true }
      name: { type: string(100), notnull: true }
      description: { type: string(255), notnull: true }
      image: { type: string(255), notnull: true }
    indexes:
      it:
  fields: item_type
    relations:
      Game: { onDelete: CASCADE, local: game_id, foreign: id, foreignAlias: GameItems }
      Price: { onDelete: CASCADE, local: price_id, foreign: id, foreignAlias: PriceItems }

我就是这样做的:

$list = MainItemTable::getInstance()->findByGameId($gameId);

$CampaignMatrix = new CampaignMatrix();

foreach($list as $index => $item) {

    $itemAssocForm = new CampaignMatrixForm($CampaignMatrix);
    $itemAssocForm->item_id = $item->getId(); // Need it in the form as hidden field
    $this->embedForm($item->getProductId(), $itemAssocForm);
}

这就是我试图获得价值的方式:

$this->widgetSchema['item_id'] = new sfWidgetFormInputText(array(), array('value' => $this->item_id)); // It doesn't get the Id

但我有一个错误: 致命错误:第237行/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php超过30秒的最长执行时间

  1. 如果我在CampaignMatrixForm中取消设置price_id,则不会产生错误。如何避免为循环中的每个项目行执行相同数据的选择?
  2. 项目ID丢失,但我需要它作为隐藏字段。如何将当前行的CampaignMatrix ID传递给CampaignMatrixForm?

3 个答案:

答案 0 :(得分:1)

您必须迭代关联以将关联表单嵌入到主窗体中。如果您发布schema.yml的一部分,它可能很简单。

尝试重新使用此代码段:

$list = MyRelatedObjectTable::getInstance()->findAll();

foreach($list as $item)
{
  $itemAssoc = AssociationTable::getInstance()->findByObjectId($this->object->id, $item->id);

  if(!$itemAssoc)
  {
    $itemAssoc = new Association();
    $itemAssoc->value_id = $itemAssoc->id;
    $itemAssoc->user_id = $this->object->id;
  }

  $itemAssocForm = new AssociationForm($itemAssoc);
  $this->embedForm('itemAssoc'.$item->code, $itemAssocForm);
}

答案 1 :(得分:0)

最好的方法是为此创建一个部分。请注意,您需要unset表单类中项目的选择框,否则您将失去关联。如果您需要更多帮助,我可以详细说明。

答案 2 :(得分:0)

你可以从缓存中获取代码并移动到你的后端/ modules / nameAPP和下一个编辑模板