5

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 ?

2

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();
  • Seems like your answer is right, why it is not accepted – Murtuza Zabuawala Oct 3 '17 at 6:05
1

You have to specify the store in order to update a page.

Load the page like this:

$pageToModify = Mage::getModel('cms/page')->setStores($storeId)->load($existingPageId);

$pageToModify->addData(array($attributeCode => $value));
0

Try this:

$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 
);
$collection = Mage::getModel('cms/page')->getCollection()->addFieldToFilter('identifier', 'testpage1');
$page = Mage::getModel('cms/page')->load($collection->getFirstItem()->getId());
$page->setData($cmsPage)->save();
  • Still the same. Creating new cms page – Marek Badura Feb 12 '14 at 14:34
  • @MarekBadura. I've updated the answer. try this new approach. – Marius Feb 12 '14 at 14:38
  • Still the same - it creating new cms page – Marek Badura Feb 12 '14 at 14:44
  • I made something like this: $page = Mage::getModel('cms/page')->load('testpage1','identifier'); if ($page->getId()) { $page->setData($cmsPage)->save(); } and it still creating new one – Marek Badura Feb 12 '14 at 15:19
  • When i made this: $page = Mage::getModel('cms/page')->load('testpage1','identifier'); if ($page->getId()) { } else { $page->setData($cmsPage)->save(); } It does nothing - so this save() action make new one – Marek Badura Feb 12 '14 at 15:21
-1

You need to do addData(); like:

$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 
    );


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

$page->addData($cmsPage)
        ->setStores(array(0))
        ->save();

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.