Ask your Symfony questions! Pay money and get answers fast! (more info)

Customizing child item form with embedRelation doctrine Symfony

  • SOLVED

Hi,

I am using symfony 1.3 and doctrine 1.2.

I have an embedRelation setup, with order and order_item in a schema somewhat like below (i have abbreviated):


Orders:
columns:
id: { type: integer(4), primary: true, autoincrement: true }
OrderItem:
columns:
id: { type: integer(4), primary: true, autoincrement: true }
order_id: { type: integer(4), primary: true, autoincrement: true }
product_id: { type: integer(4), primary: true, autoincrement: true }
quantity: { type: decimal(14) }
price: { type: decimal(10) }
relations:
Order: { local: order_id, foreign: id }
Product: { local: product_id, foreign: id }
Product:
columns:
id: { type: integer(4), primary: true, autoincrement: true }
name: { type: string(150) }


In a specific instance using my orders form and order_item form, I have created an embedRelation form to work with in a specific instance. <em>I want to modify the orderItem listings in the embedRelation form so that it only displays product_id and price</em>. However, presently when I update it, it overwrites "quantity" with blank value if I do not include it*. HOWEVER, I cannot modify orderItem form directly to unset values, as in other instances I need the full order_item form (at least this is what I have encountered thus far; perhaps someone else knows a better way).

I have lib/model/doctrine/OrderAssignForm.class.php

class OrderAssignForm extends OrdersForm
{
public function configure()
{
$this->embedRelation('OrderItem');
}
}


In form templates, I do (snippet of order item area)

<form action="<?php echo url_for('order_assign').'?id='.$form->getObject()->getId()?>">
<?php echo $form->renderHiddenFields() ?>
<?php echo $form->renderGlobalErrors() ?>

<?php foreach ($form['OrderItem'] as $oi): ?>
<?php echo $oi->renderHiddenFields() ?>
<?php echo $oi['product_id']?>
<?php echo $oi['price']->renderError() ?>
<?php echo $oi['price'] ?><br/>
<?php endforeach; ?>


But this causes quantity to be overwritten as null.

You *must* provide code to receive the award.

Answers (1)

2010-05-27

Wojciech Sznapka answers:

Use inheritance :-)
Create EmbedOrderItemForm in lib/form/doctrine in which you will customize it, and then use as embedded form


class EmbedOrderItemForm extends OrderItemForm
{
public function configure()
{
$this->useFields(array('displays product_id, 'price'));
}
}


when embeding relation remember to pass form class:

$this->embedRelation ('OrderItem', 'EmbedOrderItemForm');


webguy comments:

Of course! I was trying to do this, just didn't know the syntax. Thank you!