0

有没有办法在使用直接 SQL 的脚本中转换使用 magento 模型和集合的脚本。

我知道这一切都是可能的:),问题是一种简单直观的方式,让我从脚本中获取查询,然后在直接数据库脚本中重用。

为什么 ?我希望有一个更快的脚本。

代码示例?

假设这个脚本:

function getProd() {

$collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addTierPriceData();

return $collection;
}
function getTier($prod) {

$t_1 = false;

$_tierPrices = $prod->getTierPrice();

if ($_tierPrices) {

    $_firstTier = array_slice($_tierPrices, 0, 1);

    if (isset($_firstTier[0]))
        $t_1 = $_firstTier[0]['price'];
}

return $t_1;
}

function exec() {

$collection = getProd();

$i = 0;

foreach ($collection as $prod) {

    $class = ($i % 2 == 0) ? '' : 'class="odd"';
    $t1 = 0;
    if ($t1 = getTier($prod)) {

            $prod = mage::getModel('catalog/product')->load($prod->getId());

            $prod->setPrice($t1);
            $prod->save();
            if ($safe)
                set_time_limit(60);
        }
    }
}

exec();

基本上我想处理所有具有等级价格的产品,并将产品的主要价格替换为等级价格。

该脚本在 12000 个产品上花费了 9 多个小时,所以我猜 SQL 方法会更快,但需要我研究数据库结构。

因此,如果有人有一种很好的方法可以在 PHP/SQL 脚本中进行转换,那就太好了(更多只打印集合查询的东西)

4

1 回答 1

0

如果您只需要一个简单的一次性解决方案来查看查询是什么,您应该能够执行类似echo $collection->getSelect();. 集合上的getSelect函数将返回一个Zend_Select对象。此类具有__toString返回当前构建查询的 sql 的函数。

于 2012-12-12T10:18:53.260 回答