Answers:
好的,所以我对此没有太多反应,所以开始研究自己可以回答我提出的一半问题。
做一个简单的“ grep”,遇到了藏在lib文件夹中的以下类: lib/Magento/Db/Sql/Trigger.php
它非常简单,这就是我想出的功能(可以放在您的安装/升级脚本中):
<?php
/** @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
// Trigger
$trigger = new Magento_Db_Sql_Trigger();
// Set time SQL_TIME_BEFORE / SQL_TIME_AFTER
$trigger->setTime($trigger::SQL_TIME_BEFORE);
// Set time SQL_EVENT_INSERT / SQL_EVENT_UPDATE / SQL_EVENT_DELETE
$trigger->setEvent($trigger::SQL_EVENT_INSERT);
// Set target table name
$trigger->setTarget($installer->getTable('fontera_trader/leaderboards_global_tmp'));
// Set Body
$trigger->setBody(
'INSERT INTO '.$installer->getTable('fontera_trader/leaderboards_global').'
(entity_id, customer_id , credit_value, prize_value, games_played, rank, prev_rank)
VALUES
(NEW.entity_id, NEW.customer_id, NEW.credit_value, NEW.prize_value, NEW.games_played, NEW.rank, NEW.prev_rank)
ON DUPLICATE KEY UPDATE
customer_id = NEW.customer_id,
credit_value = NEW.credit_value,
prize_value = NEW.prize_value,
games_played = NEW.games_played,
rank = NEW.rank,
prev_rank = NEW.prev_rank;
SET @r = 0;
UPDATE '.$installer->getTable('fontera_trader/leaderboards_global').'
SET
prev_rank = rank,
rank = @r:= (@r+1)
ORDER BY
credit_value
DESC;
'
);
// Assemble query, returns direct SQL for trigger
$triggerCreateQuery = $trigger->assemble();
// Adapter initiates query
$this->getConnection()->query($triggerCreateQuery);
$installer->endSetup();
我添加了注释,以给出可以使用的基本概念,否则最好自己去上课。该主体基本上是原始SQL,但可以使用Magento的常规方法进行编译。我已使用原始SQL进行演示。
顺便说一句,我仍在尝试找到一种无法成功执行存储过程的方法。是否有人在Magento中遇到过类似的东西,可能会藏起来以备将来使用?
$trigger->setName('my_trigger_name')
并$this->getConnection()->dropTrigger($trigger->getName())
在其之前添加$this->getConnection()->query($triggerCreateQuery);
.sql
在存储过程中使用版本文件。更多详细信息:alanstorm.com/magento_setup_resources