如何通过安装脚本更新现有的cms页面 [英] How to update existing cms page through install script
问题描述
我有这个脚本
$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屋!