我需要创建一个自定义模块,该模块会覆盖现有的“评论”模块以添加额外字段“电子邮件ID”
我相信我需要重写核心模型文件' app / code / core / Mage / Review / Model / Resource / Review.php ',' app / code / core / Mage / Review / Model / Resource / Review / Product / Collection.php '和阻止文件' app / code / core / Mage / Adminhtml / Block / Review / Edit / Form.php “
此外,我不知道应该包含哪些安装脚本以及使用哪个版本来升级现有的“review_detail”表格。
到目前为止,这是我所做的代码,它不起作用。现在我在review_detail表中手动添加了'email'字段,并在base / default / template / review / form.phtml中添加了电子邮件输入字段
CM
Review
Block
Review
Edit
Form.php
etc
config.xml
Helper
Data.php
Model
Resource
Review.php
Review
Product
Collection.php
这是我的config.xml文件
<?xml version="1.0"?>
<config>
<modules>
<CM_Review>
<version>0.0.1</version>
</CM_Review>
</modules>
<frontend>
<routers>
<review>
<use>standard</use>
<args>
<module>CM_Review</module>
<frontName>Review</frontName>
</args>
</review>
</routers>
</frontend>
<global>
<blocks>
<adminhtml>
<rewrite>
<review_edit_form>CM_Review_Block_Review_Edit_Form</review_edit_form>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<review>
<rewrite>
<class>CM_Review_Helper</class>
</rewrite>
</review>
</helpers>
<models>
<review>
<rewrite>
<resource_review>CM_Review_Model_Resource_Review</resource_review>
<resource_review_product_collection>
CM_Review_Model_Resource_Review_Product_Collection
</resource_review_product_collection>
</rewrite>
</review>
</models>
</global>
</config>
我已复制粘贴原始块和模型文件的内容,并添加了“电子邮件”字段。但仍然无法保存任何数据。但是,直接编辑原始文件可以解决问题,但不能通过我的自定义模块。
有什么明显的我错过了吗?任何帮助指针都可以。感谢。
我没有收到任何错误,但该字段既没有保存到数据库也没有通过安装脚本代码添加。因此,我在代码中评论了资源部分,并手动添加了字段。这是我的代码,
config.xml中
<?xml version="1.0"?>
<config>
<modules>
<CM_Reviewmail>
<version>0.0.1</version>
</CM_Reviewmail>
</modules>
<frontend>
<routers>
<reviewmail>
<use>standard</use>
<args>
<module>CM_Reviewmail</module>
<frontName>cmreviewmail</frontName>
</args>
</reviewmail>
</routers>
</frontend>
<global>
<blocks>
<adminhtml>
<rewrite>
<review_edit_form>CM_Reviewmail_Block_Review_Edit_Form</review_edit_form>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<reviewmail>
<class>CM_Reviewmail_Helper</class>
</reviewmail>
</helpers>
<models>
<reviewmail>
<rewrite>
<resource_review>CM_Reviewmail_Model_Resource_Review</resource_review>
</rewrite>
</reviewmail>
</models>
<!-- <resources>
<reviewmail_setup>
<setup>
<module>CM_Reviewmail</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</reviewmail_setup>
<reviewmail_setup_write>
<connection>
<use>core_write</use>
</connection>
</reviewmail_setup_write>
<reviewmail_setup_read>
<connection>
<use>core_read</use>
</connection>
</reviewmail_setup_read>
</resources>-->
</global>
</config>
这是我的Reviewmail / Block / Review / Edit / Form.php代码
<?php
class CM_Reviewmail_Block_Review_Edit_Form extends Mage_Adminhtml_Block_Review_Edit_Form
{
protected function _prepareForm()
{
$review = Mage::registry('review_data');
$product = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
$customer = Mage::getModel('customer/customer')->load($review->getCustomerId());
$statuses = Mage::getModel('review/review')
->getStatusCollection()
->load()
->toOptionArray();
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'), 'ret' => Mage::registry('ret'))),
'method' => 'post'
));
$fieldset = $form->addFieldset('review_details', array('legend' => Mage::helper('review')->__('Review Details'), 'class' => 'fieldset-wide'));
$fieldset->addField('product_name', 'note', array(
'label' => Mage::helper('review')->__('Product'),
'text' => '<a href="' . $this->getUrl('*/catalog_product/edit', array('id' => $product->getId())) . '" onclick="this.target=\'blank\'">' . $product->getName() . '</a>'
));
if ($customer->getId()) {
$customerText = Mage::helper('review')->__('<a href="%1$s" onclick="this.target=\'blank\'">%2$s %3$s</a> <a href="mailto:%4$s">(%4$s)</a>',
$this->getUrl('*/customer/edit', array('id' => $customer->getId(), 'active_tab'=>'review')),
$this->htmlEscape($customer->getFirstname()),
$this->htmlEscape($customer->getLastname()),
$this->htmlEscape($customer->getEmail()));
} else {
if (is_null($review->getCustomerId())) {
$customerText = Mage::helper('review')->__('Guest');
} elseif ($review->getCustomerId() == 0) {
$customerText = Mage::helper('review')->__('Administrator');
}
}
$fieldset->addField('customer', 'note', array(
'label' => Mage::helper('review')->__('Posted By'),
'text' => $customerText,
));
$fieldset->addField('summary_rating', 'note', array(
'label' => Mage::helper('review')->__('Summary Rating'),
'text' => $this->getLayout()->createBlock('adminhtml/review_rating_summary')->toHtml(),
));
$fieldset->addField('detailed_rating', 'note', array(
'label' => Mage::helper('review')->__('Detailed Rating'),
'required' => true,
'text' => '<div id="rating_detail">' . $this->getLayout()->createBlock('adminhtml/review_rating_detailed')->toHtml() . '</div>',
));
$fieldset->addField('status_id', 'select', array(
'label' => Mage::helper('review')->__('Status'),
'required' => true,
'name' => 'status_id',
'values' => Mage::helper('review')->translateArray($statuses),
));
/**
* Check is single store mode
*/
if (!Mage::app()->isSingleStoreMode()) {
$fieldset->addField('select_stores', 'multiselect', array(
'label' => Mage::helper('review')->__('Visible In'),
'required' => true,
'name' => 'stores[]',
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm()
));
$review->setSelectStores($review->getStores());
}
else {
$fieldset->addField('select_stores', 'hidden', array(
'name' => 'stores[]',
'value' => Mage::app()->getStore(true)->getId()
));
$review->setSelectStores(Mage::app()->getStore(true)->getId());
}
$fieldset->addField('email', 'text', array( //CUSTOM field
'label' => Mage::helper('review')->__('Email'),
'required' => true,
'name' => 'email'
));
$fieldset->addField('nickname', 'text', array(
'label' => Mage::helper('review')->__('Nickname'),
'required' => true,
'name' => 'nickname'
));
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('review')->__('Summary of Review'),
'required' => true,
'name' => 'title',
));
$fieldset->addField('detail', 'textarea', array(
'label' => Mage::helper('review')->__('Review'),
'required' => true,
'name' => 'detail',
'style' => 'height:24em;',
));
$form->setUseContainer(true);
$form->setValues($review->getData());
$this->setForm($form);
return parent::_prepareForm();
}
}
这是我的Reviewmail / Model / Resources / Review.php代码
<?php
class CM_Reviewmail_Model_Resource_Review extends Mage_Review_Model_Resource_Review
{
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$adapter = $this->_getWriteAdapter();
/**
* save detail
*/
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'email' => $object->getEmail(),
);
$select = $adapter->select()
->from($this->_reviewDetailTable, 'detail_id')
->where('review_id = :review_id');
$detailId = $adapter->fetchOne($select, array(':review_id' => $object->getId()));
if ($detailId) {
$condition = array("detail_id = ?" => $detailId);
$adapter->update($this->_reviewDetailTable, $detail, $condition);
} else {
$detail['store_id'] = $object->getStoreId();
$detail['customer_id']= $object->getCustomerId();
$detail['review_id'] = $object->getId();
$adapter->insert($this->_reviewDetailTable, $detail);
}
/**
* save stores
*/
$stores = $object->getStores();
if (!empty($stores)) {
$condition = array('review_id = ?' => $object->getId());
$adapter->delete($this->_reviewStoreTable, $condition);
$insertedStoreIds = array();
foreach ($stores as $storeId) {
if (in_array($storeId, $insertedStoreIds)) {
continue;
}
$insertedStoreIds[] = $storeId;
$storeInsert = array(
'store_id' => $storeId,
'review_id'=> $object->getId()
);
$adapter->insert($this->_reviewStoreTable, $storeInsert);
}
}
// reaggregate ratings, that depend on this review
$this->_aggregateRatings(
$this->_loadVotedRatingIds($object->getId()),
$object->getEntityPkValue()
);
return $this;
}
}
有一个Helper / Data.php
<?php
class CM_Reviewmail_Helper_Data extends Mage_Core_Helper_Abstract {}
这是Model / Resource / Mysql4 / Setup.php
<?php
class CM_Reviewmail_Model_Resource_Mysl4_Setup extends Mage_Core_Model_Resource_Setup {}
任何人都可以指出我做错了什么吗?还有什么需要做的?
请任何帮助表示赞赏。我一直在撞墙,需要尽快解决这个问题。提前谢谢。
答案 0 :(得分:2)
重写类的方法看起来大部分是正确的,但请记住,您正在重写adminhtml/review_edit_form
以指向CM_Review_Block_Review_Edit_Form,因此在app/code/local/CM/Review/Block/Review/Edit/Form.php
中您需要声明该类:
<?php
class CM_Review_Block_Review_Edit_Form extends Mage_Adminhtml_Block_Review_Edit_Form
{
然后只覆盖你需要的功能。
不知道你是否已经这样做了。同一过程适用于您重写的任何模型。你不应该真的需要重写这个集合,但这取决于你需要做什么。
关于安装内容:
app/code/local/CM/Review/etc/config.xml
中的
<config>
...
<resources>
<review_setup>
<setup>
<module>CM_Review</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</review_setup>
</resources>
...
</config>
在app/code/local/CM/Review/sql/review_setup/mysql4-install-0.0.1.php
<?php
$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE review_detail ADD COLUMN email_id INT NULL");
$installer->endSetup();
如果这不是第一次(它应该,除非你已经尝试了类似的东西),进入数据库,并从表“{{1}”中删除“review_setup
”行}”。然后再试一次。添加列后,请务必记住在core_resource
不要忘记您还需要在System > Cache Management
中创建名为CM_Review.xml
的文件。它应该是这样的:
app/etc/modules
希望这有帮助。
答案 1 :(得分:2)
我弄清楚我做错了什么。我没有在config.xml中正确声明我的资源文件。这就是我现在的做法。
<models>
<review_resource>
<rewrite>
<review>CM_Reviewmail_Model_Resource_Review</review>
<collection>CM_Reviewmail_Model_Resource_Review_Collection</collection>
<product_collection>CM_Reviewmail_Model_Resource_Review_Product_Collection</product_collection>
</rewrite>
</review_resource>
</models>
然后我创建了上面提到的文件,确保扩展原始类并覆盖需要更改的函数。最后,我能够使用注册/访客客户的电子邮件ID保存数据库中的字段。
但是,当我尝试使用以下查询获取这些电子邮件值时,
Mage::getModel('review/review')->getCollection()
->addFieldToFilter('email', $emailId)
->addFieldToFilter('status_id' , '1')
->addFieldToSelect('email')->getData();
它会给我任何回报。当我在phpMyAdmin中执行它时,我得到了这个查询
SELECT `main_table`.`email`, `detail`.`detail_id`, `detail`.`title`, `detail`.`detail`, `detail`.`nickname`, `detail`.`customer_id` FROM `review` AS `main_table` INNER JOIN `review_detail` AS `detail` ON main_table.review_id = detail.review_id WHERE (email = 'test@test.com') AND (status_id = '1')
错误地说明了列表中的“未知main_table.email
字段”。
现在任何人都可以帮我指出问题,以便为什么保存数据但不让我抓取它?为什么它在保存数据时无法识别我的字段?我的查询错了吗?
PS:但是如果我在phpMyAdmin的上述查询中用'email'替换'main_table.email'并执行它,它会返回正确的值。那么有没有办法通过废除'main_table'部分在代码中编写查询?
答案 2 :(得分:0)
这是错误
<product_collection>CM_Reviewmail_Model_Resource_Review_Product_Collection</product_collection>
应该是
<review_product_collection>CM_Reviewmail_Model_Resource_Review_Product_Collection</review_product_collection>