Magento – How to update existing cms page through install script

magento-1.8setup-script

I have this script

<?php
$cmsPage = Array ( 

                    'title' => 'Test Page 1',
                    'root_template' => 'one_column', 
                    'identifier' => 'testpage1', 
                    'content' => "<p>Testowa sprawa czy dziaƂa update ? oooooooooooo</p>", 
                    'is_active' => 1,
                    'stores' => array(1), 
                    'sort_order' => 0 
    );


Mage::getModel('cms/page')->setData($cmsPage)->save();

If cms page with identifier "testpage1" exist , the script create another with the same identifier.

Is there a way to check if cmspage exist – and if that is true – do update ?

Best Answer

I came accross the exact same problem on Magento 1.9.2.2

I couldn't find why, but every time I wanted to update a specific cms page, it was creating a new one instead. I finally checked the controller code, and after seeing it was the same code I was using, I checked the data sent in the form. And the answer is the page_id.

Try setting the page_id of your current page in the data, and it should work.

$page = Mage::getModel('cms/page')
    ->load($identifier, 'identifier');

$pageData = array(
    'title' => 'Test Page 1',
    'root_template' => 'one_column',
    'identifier' => 'testpage1',
    'stores' => array(0),
    'content' => $content,
    'layout_update_xml' => $design,
    'page_id' => $page->getId()
);

$page->setData($pageData)
    ->save();
Related Topic