如何通过安装脚本更新现有的cms页面 [英] How to update existing cms page through install script

查看:86
本文介绍了如何通过安装脚本更新现有的cms页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本

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

如果存在标识符为"testpage1"的cms页面,则脚本将创建另一个具有相同标识符的页面.有没有一种方法可以检查cmspage是否存在-如果是,则进行更新吗?

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 ?

推荐答案

调用$page->setData($cmsPage)时,将从Mage_Cms_Model_Page对象中删除所有数据,并将其替换为$cmsPage数组中的数据.您要删除的部分数据是对象内部$_data数组中的page_id. page_id将对象映射到cms_page数据库表中的列.通过删除该ID,您可以使Magento运行INSERT查询而不是UPDATE.

When you call $page->setData($cmsPage) you remove all data from the Mage_Cms_Model_Page object and replace it with the data from the $cmsPage array. Part of the data you are removing is the page_id from the object's internal $_data array. The page_id is what maps the object to a column in the cms_page database table. By removing that ID you make Magento run an INSERT query rather than an UPDATE.

类似的事情应该起作用:

Something like this should work:

$pageId = $collection = Mage::getModel('cms/page')->getCollection()->addFieldToFilter('identifier', 'testpage1')->getFirstItem()->getId();
$page = Mage::getModel('cms/page')->load($pageId);

$cmsPage = Array ( 
    'page_id' => $pageId,
    '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->setData($cmsPage)->save();

这篇关于如何通过安装脚本更新现有的cms页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆