0

We're having some problems with our upgrade script updating the store. The error that we get is:

A block identifier with the same properties already exists in the selected store.

I've post the code in our upgrade script in hopes that someone might be able to point out where the mistake was made:

/**
 * Get all store views associated with our brand
 * @var $stores Mage_Core_Model_Store
 * @return mixed
 * @since 0.1.0
 */
$stores = Mage::getModel('core/store')
    ->getCollection()
    ->addFieldToFilter(
        'code', array(
            'like'=> "brand_%"
        )
    )->getAllIds();

/**
 * Get all Brand About navigation blocks
 *
 * @var $navs Mage_Cms_Model_Block
 * @return mixed
 * @since 0.1.0
 */
$navBlocks = Mage::getModel('cms/block')
    ->load('brand_nav_about')
    ->getCollection()
    ->addFieldToFilter('identifier', array( 'like' => 'brand_nav_about' ) );

// Loop over each store
foreach( $stores as &$store ) {

    // Loop through each navigation block
    foreach( $navBlocks as $navBlock ) {

        $staticBlock = Mage::getModel('cms/block')
            ->setStoreId( $store )
            ->load( $navBlock->getIdentifier(), 'identifier');

        if( $staticBlock->getId() ) {
            $staticBlock
                ->setStores(array($store))
                ->save();
        }
    }
}
  • Please explain what this script is supposed to do. – Fabian Schmengler Mar 16 '16 at 10:42
  • Basically we had an update script that ran and created the block 'brand_nav_about' but it didn't assign the country. So now we wanted to have another upgrade script go through and apply a store view (country) to each. – Stephen Johnston Mar 16 '16 at 16:26
3

The reason why the issue occurs is that you are trying to save several blocks that have one and the same identificators and store_id -s, which is strictly forbidden to use in Magento.

You can simultaneously save only several blocks with DIFFERENT store_id

Here is the result of the check that contains this error:

result

result 2

You should change the code the following way:

  • in the cycle for each store load the blocks with filters for this particular store

  • Next save all, setting as store_id this particular store

result 3

Here is the code:

<?php
include_once('app/Mage.php');
Mage::app();

/**
 * Get all store views associated with our brand
 * @var $stores Mage_Core_Model_Store
 * @return mixed
 * @since 0.1.0
 */
$stores = Mage::getModel('core/store')
    ->getCollection()
    ->addFieldToFilter(
        'code', array(
            'like' => "brand_%"
        )
    );

foreach ($stores as $store) {
    /**
     * Get all Brand About navigation blocks
     *
     * @var $navs Mage_Cms_Model_Block
     * @return mixed
     * @since 0.1.0
     */
    $navBlocks = Mage::getModel('cms/block')
        ->getCollection()
        ->addFieldToFilter('identifier', array('like' => '%brand_nav_about%'))
        ->addStoreFilter($store->getId());

    // Loop through each navigation block
    /** @var Mage_Cms_Model_Block $navBlock */
    foreach ($navBlocks as $navBlock) {

        $navBlock->save(); // or do something else...

        /*
        $staticBlock = Mage::getModel('cms/block')
            ->load($navBlock->getIdentifier(), 'identifier');

        if ($staticBlock->getId()) {
            $staticBlock
                ->setStores($stores)
                ->save();
        }*/
    }
}
| improve this answer | |
2

Try

/**
 * Get all store views associated with our brand
 * @var $stores Mage_Core_Model_Store
 * @return mixed
 * @since 0.1.0
 */
$stores = Mage::getModel('core/store')
    ->getCollection()
    ->addFieldToFilter(
        'code', array(
            'like'=> "brand_%"
        )
    )->getAllIds();

/**
 * Get all Brand About navigation blocks
 *
 * @var $navs Mage_Cms_Model_Block
 * @return mixed
 * @since 0.1.0
 */
$navBlocks = Mage::getModel('cms/block')
    ->getCollection()
    ->addFieldToFilter('identifier', array( 'like' => '%brand_nav_about%') );


    // Loop through each navigation block
    foreach( $navBlocks as $navBlock ) {

        $staticBlock = Mage::getModel('cms/block')
           ->load( $navBlock->getIdentifier(), 'identifier');

        if( $staticBlock->getId() ) {
            $staticBlock
                ->setStores($stores)
                ->save();
        }
    }

Take a look at app/code/core/Mage/Cms/Model/Resource/Block.php

| improve this answer | |
  • That only seems to update one block. We have three store views 'en', 'ca', and 'fr'. To which we have three blocks that we can update with the appropriate translations. – Stephen Johnston Mar 15 '16 at 23:28
  • Is it working correctly but only for 1 block id? – Renon Stewart Mar 15 '16 at 23:52
  • Yes, that is correct @R.S – Stephen Johnston Mar 16 '16 at 0:03
  • Is "brand_nav_about" the block that work? Try removing ->load('brand_nav_about'). Try do some debugging of $navBlocks to see what collection it return. Also change array( 'like' => 'brand_nav_about') to array( 'like' => '%brand_nav_about%') – Renon Stewart Mar 16 '16 at 10:37
0

You don't need a collection load to load the respective block via identifier. You can just use the load function nowadays:

$oBlock = Mage::getModel('cms/block')->load(
    $aData['identifier'], 'identifier'
);

For this particular usage case, I wrote an tutorial before a view rears. I've updated it because of your post here: https://www.ask-sheldon.com/install-cms-blocks-with-your-module/

This is just a addition for the other answers.

| improve this answer | |

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.