Roman Newaza
Roman Newaza

Reputation: 11690

Modification of Admin Generator's Doctrine Form layout

I need to modify Admin Generator's Doctrine Form which is included by:

$this->embedRelation('MyRelation');

The default layout looks like this:

Screenshot 1

The goal - every Item of Select should be displayed as text in separate row, plus Price and Quantity:

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 }

This is how I do it:

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

And this is how I'm trying to get the value:

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

But I have an error: Fatal error: Maximum execution time of 30 seconds exceeded in /vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php on line 237

  1. If I unset price_id in CampaignMatrixForm, no error produced. How to avoid execution of select of the same data for every item row in the loop?
  2. Item id is missing, but I need it as hidden field. How to pass CampaignMatrix ID of current row to CampaignMatrixForm?

Upvotes: 0

Views: 439

Answers (3)

dxb
dxb

Reputation: 931

You have to iterate on association to emebed the association form to the main form. It mays be simple if you post a part of your schema.yml.

Try to re-use this snippet :

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

Upvotes: 1

Paul Attuck
Paul Attuck

Reputation: 2269

you can get code from cache and move to your backend/modules/nameAPP and next edit template

Upvotes: 0

samura
samura

Reputation: 4395

The best way is to create a partial for that. Be aware that you need to unset the select box for the Item in the Form class or you'll lose your association. I can elaborate on that if you need more help.

Upvotes: 0

Related Questions