Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Right way to develop model working with parent-child tables
 
Ryan Sun _Kuafu
Guru
 
Total Posts:  317
Joined:  2008-11-14
FL
 

We have a large set of data need to be insert to several cascaded tables, right now I transformed the data into multiple dimensional array and use foreach loops to insert into different tables, like this:

foreach($this->_dData[’a’] as $aItem)
{
$aModel = Mage::getModel(’a’);
$aData = array(’filed1’ => ‘value1’, ‘filed2’=>’value2’);
$aModel ->addData($aData)->save();
$aId = $aModel->getId();
foreach($partItem[’b’] as $bItem)
{
$bModel = Mage::getModel(’b’);
$bData = array(’forgionkey’ => aId, ‘filed1’ => ‘value1’, ‘filed2’=>’value2’);
$bModel ->addData($bData)->save();
$bId = $bModel->getId();
foreach($regionItem[’c’] as $cItem)
{
$cModel = Mage::getModel(’c’);
$cData = array(’forgionkey’ => bId, ‘filed1’ => ‘value1’, ‘filed2’=>’value2’);
$cModel ->addData($cData)->save();
}
}

This piece of code dosen’t seem quite right to me,
Do we have a better way?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top