Question

I have a Magento standard admin form and it works fine for editing one collection record, but I want to make it so I can edit multiple collection records at once.

My idea was to do a for loop where it adds the fields:

for($i=1;$i<=$productsInOrder;$i++){

    $fieldset->addField('start_date' . $i

and add indexes to the field names, but I don't know how to properly modify the collection query and setting form data to account for the new indexes. The table I am pulling records from and modifying is a simple custom table with extra order info in it for each product.

Any ideas? Has anyone done this before and can share their code?

Was it helpful?

Solution

I ended up setting manually the data fields like this at the top of the form. The $data will be an array if you get a collection for the form edit page:

  /**
         * Custom code to be used for $form->setValues as an array from $data
         * $valueData['data_key' . $index] = value
         */

        if (isset($data[$i]) && is_array($data[$i])) {
            $valArr = $data[$i];
        } else {
            $valArr = $data;
        }
        foreach ($valArr as $datakey => $dataitem) {
            $valueData[$datakey . $i] = $dataitem;
        }

The form fields were like this:

 $fieldset->addField(
            'start_date' . $i, 'text', array(
                'label'        => Mage::helper('payperrentals')->__('Start Date'),
                'class'        => 'required-entry startdate',
                'name'         => 'start_date' . $i,
                'title'        => $i
            )
        );

Then on the end of the form I set it with:

 $form->setValues($valueData);

Finally on the controller saveAction I used something like this:

for ($i = 0; $i < $numberOfProducts; $i++) {
$reservationOrderItem = Mage::getModel('payperrentals/reservationorders')->load($entityid);
$reservationOrderItem->setStartTurnoverBefore(ITwebexperts_Payperrentals_Helper_Date::toMysqlDate
($turnoverAr['before']))
    ->setEndTurnoverAfter(ITwebexperts_Payperrentals_Helper_Date::toMysqlDate
    ($turnoverAr['after']))
    ->setQty($data['qty' . $i])
    ->setProductId($data['product_id' . $i])
    ->setStartDate($data['start_date' . $i])
    ->setEndDate($data['end_date' . $i])
    ->setProductType('reservation')
    ->setComments($data['comments' . $i]);

if ($entityid) {
    $reservationOrderItem->setId($entityid);
}
$reservationOrderItem->save(); }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top