Domanda

I have a query regarding magento 1.6.

I have a number of objects that I am creating, calling a method on and saving to the database with the Mage_Core_Model_Resource_Transaction transaction model.

The method now requires the primary key value of the object, which obviously isn't going to be available until after the object has been saved to the database.

Can I just loop through the created objects after they've been saved to the database? Instead, do I need to use the transaction's addCommitCallback method?

In other words, can I do:

$collection = [];
$transaction = Mage::getModel('core/resource_transaction');
for ($i = 0; $i <= 10; $i++) {
    $obj = new ObjectFoo;
    $transaction->addObject($obj);
    $collection[] = $obj;
}
$transaction->save();
foreach($collection as $collected) {
    $this->doSomething($collected);
}

(And assume that each $collected item will now have a valid id value)

Or do I need to use something like:

$transaction->addCommitCallback(array($obj, 'doSomething'));

One thing that I have noticed with addCommitCallback is that you can not specify any parameters for the callback, which is rather... restrictive.

È stato utile?

Soluzione

Yes, each collected item should contain the id after saving the transaction.

http://www.php.net/manual/en/language.oop5.references.php

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top