1

This is the implementation of getChildrenIds in the class Mage_Catalog_Model_Resource_Product_Type_Configurable:

/**
 * Retrieve Required children ids
 * Return grouped array, ex array(
 *   group => array(ids)
 * )
 *
 * @param int $parentId
 * @param bool $required
 * @return array
 */
public function getChildrenIds($parentId, $required = true)
{
    $childrenIds = array();
    $select = $this->_getReadAdapter()->select()
        ->from(array('l' => $this->getMainTable()), array('product_id', 'parent_id'))
        ->join(
            array('e' => $this->getTable('catalog/product')),
            'e.entity_id = l.product_id AND e.required_options = 0',
            array()
        )
        ->where('parent_id = ?', $parentId);

    $childrenIds = array(0 => array());
    foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
        $childrenIds[0][$row['product_id']] = $row['product_id'];
    }

    return $childrenIds;
}

As result from this function you get e.g.:

$childrenIds = array(
    0 => array(
        1 => "1",
        2 => "2",
        ...
    )
);

If I want to process this data, I always access $childrenIds[0] because $childrenIds always has at most one item so no need to iterate over it.

Why is result an encapsulated array, and not a simple array?

Wouldn't it be better to replace

$childrenIds = array(0 => array());
foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
    $childrenIds[0][$row['product_id']] = $row['product_id'];
}

with the following code?

$childrenIds = array();
foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
    $childrenIds[$row['product_id']] = $row['product_id'];
}

What advantage does the actual implementation have?

  • 1
    I have the same question. It took me a while to figure out that I had to reference the array inside the array, though. :( – Michael Yaeger Nov 22 '16 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.