1

I will try get all pages assign to the website with name

$collection = Mage::getModel('cms/page')->getCollection();
$collection->getSelect()
   ->join(
       array('cps' => $collection->getTable('cms/page_store')),
       'cps.page_id = main_table.page_id',
       array('store_id')
   );

but after the query I receive on my page such message

Item (Mage_Cms_Model_Page) with the same id "9" already exist

as a result I want receive something like that

About Us    about-magento-demo-store    2 columns with right bar    All Store Views Enabled Mar 13, 2015 1:30:07 AM Mar 13, 2015 1:30:07 AM
...
2

This actually already happens from within the collection you are assembling. This is why you are receiving the error.

If you look in app/code/core/Mage/Cms/Model/Resource/Page/Collection.php there is a method called _afterLoad(). This is where the joining of the table is done. This method is a hook method called when loading collections. It also happens on resources as well.

Here is the _afterLoad() method:

/**
     * Perform operations after collection load
     *
     * @return Mage_Cms_Model_Resource_Page_Collection
     */
    protected function _afterLoad()
    {
        if ($this->_previewFlag) {
            $items = $this->getColumnValues('page_id');
            $connection = $this->getConnection();
            if (count($items)) {
                $select = $connection->select()
                        ->from(array('cps'=>$this->getTable('cms/page_store')))
                        ->where('cps.page_id IN (?)', $items);

                if ($result = $connection->fetchPairs($select)) {
                    foreach ($this as $item) {
                        if (!isset($result[$item->getData('page_id')])) {
                            continue;
                        }
                        if ($result[$item->getData('page_id')] == 0) {
                            $stores = Mage::app()->getStores(false, true);
                            $storeId = current($stores)->getId();
                            $storeCode = key($stores);
                        } else {
                            $storeId = $result[$item->getData('page_id')];
                            $storeCode = Mage::app()->getStore($storeId)->getCode();
                        }
                        $item->setData('_first_store_id', $storeId);
                        $item->setData('store_code', $storeCode);
                    }
                }
            }
        }

        return parent::_afterLoad();
    }

I hope this helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.