Magento product model is read only to frontend controller

最近我写了一个小程序:Deal of the day for Magento。

它主要有以下功能:
每天定时按预设权重随机抽取一个产品作为 deal of the day;在产品名称前加上 “Deal of the day:” 字样;设定它的 is_deal_of_the_day 为 true;设定一个 special price;归类到一个 sales category 下;同时把昨天的已过时的 deal 重置为原先状态。

Magento 支持 cronjob 我是知道的,但测试的第一个环节倒不是这个程序能否定时启动,而是程序能否完成独立完成这一系列的操作。为求简单,我把整个过程写在一个 frontend controller 的 indexAction 里,然后用访问 url 的方式去触发这个过程。我没有用 backend controller,因为我考虑到 Magento wiki 上介绍的 cronjob 万一不管用(之前我没有用过 Magento conjob),我还可以用 curl + linux crontab (这个我很熟)去触发这个过程。当然我会把这个 url 做得谁都猜不到,以策安全。

但是,在 frontend controller 里一运行到 $product->save() 或 $productCollection->save(),就抛出以下错误:

Warning: Invalid argument supplied for foreach() in /path/to/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 995

初看这个错误,我总以为是 product EAV 某个 attribute 损坏了,因为我经常修改 product attributes,一不小心,改坏了哪个不该改的 attribute 也不奇怪。可难就难在 attributes 那么多,我无法知道哪个坏了啊。我围绕着 Abstract.php 995 行左看右看,没有头绪整整一个下午,最后发现,如果把同样代码放在 backend controller 里,product 就可以 save 了。

整整一个下午的时间只给我一个启示:product model 在 frontend 是只读的,估计又属于 Magento 安全机制范畴。联想到以前碰到的一个问题:如果用户在 Magento backend 已登录,能否在 frontend 探知,以便针对 backend 用户访问 frontend 时显示不对外人开放的内容(就像 wordpress 那样会在每个 post 显示 edit link)。但 Magento 把 backend 和 frontend 严格地分开,我研究好久,也没有让 frontend 访问到 backend session。

我想,打破 Magento 苦心经营的安全机制去做一些事是不可取的。比如 deal of the day 定时切换的功能,就用 magento 鼓励的 cron.php 去启动,非常的方便,也非常的安全。只是我空下来还是想钻牛角尖: Magento 是怎么把 backend 和 frontend 严格地分开的?只有知道了原理,如果、万一、假如、倘若我要 frontend 执行 backend 操作,也能做到了。

Leave a comment

Your email address will not be published. Required fields are marked *