Cakephp迁移:重命名字段并更新数据库中的值

时间:2017-01-05 13:04:49

标签: php cakephp cakephp-2.0

我正在尝试重命名字段并更新cakephp迁移中已插入行的值。

工作流:
在之前的方法中,我在我的数据库中获取所有已添加的行,这样我就可以在方法之后更新它们 然后进行迁移,从而创建列value_from和value_to,并删除列price_from和price_to
然后我尝试在我的after方法中获取所有“新”行,我不想更新旧的值,但是会发生错误,因为find('all')方法会抛出错误

我的代码:     

class ShippingTypeCostCalculationM1483610977ProjectPluginLogistics extends CakeMigration {

    /**
     * Data which will be inserted in modified table shipping costs
     * @var
     */
    private $data;

/**
 * Migration description
 *
 * @var string
 * @access public
 */
    public $description = '';

/**
 * Actions to be performed
 *
 * @var array $migration
 * @access public
 */
    public $migration = array(
        'up' => array(
            'create_field' => array(
                'shipping_costs' => array(
                    'shipping_type' => array('type' => 'string', 'null' => false, 'default' => 'order_sum', 'length' => 20, 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8', 'after' => 'customer_type'),
                    'value_from' => array('type' => 'decimal', 'null' => false, 'default' => NULL, 'length' => '10,4', 'after' => 'shipping_type'),
                    'value_to' => array('type' => 'decimal', 'null' => false, 'default' => NULL, 'length' => '10,4', 'after' => 'value_from'),
                ),
            ),
            'drop_field' => array(
                'shipping_costs' => array('price_from', 'price_to',),
            )
        ),
        'down' => array(
            'drop_field' => array(
                'shipping_costs' => array('shipping_type', 'value_from', 'value_to',),
            ),
        ),
        'create_field' => array(
            'shipping_costs' => array(
                'price_from' => array('type' => 'decimal', 'null' => false, 'default' => NULL, 'length' => '10,4'),
                'price_to' => array('type' => 'decimal', 'null' => false, 'default' => NULL, 'length' => '10,4'),
            ),
        )
    );

/**
 * Before migration callback
 *
 * @param string $direction, up or down direction of migration process
 * @return boolean Should process continue
 * @access public
 */
    public function before($direction) {
        $shippingCost = ClassRegistry::init('Logistics.ShippingCost');
        $this->data = $shippingCost->find('all');

        return true;
    }

/**
 * After migration callback
 *
 * @param string $direction, up or down direction of migration process
 * @return boolean Should process continue
 * @access public
 */
    public function after($direction) {

        $shippingCost = ClassRegistry::init('Logistics.ShippingCost');
        // This is where error happens
        $shippingCosts = $shippingCost->find('all');

        foreach ($this->data as $item) {
            $shippingCost = $shippingCosts->get($item['shipping_costs']['id']);
            $shippingCost->value_from = $item['shipping_costs']['price_from'];
            $shippingCost->value_to = $item['shipping_costs']['price_to'];
            $shippingCosts->save($shippingCost);
        }

        return true;
    }
}

问题: 在after方法中,cake php仍在尝试获取已经删除的price_from或price_to等值。

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ShippingCost.price_from' in 'field list'   

我如何克服自己的问题? 如果您需要任何其他信息,请告诉我,我会提供。提前谢谢!

1 个答案:

答案 0 :(得分:2)

在任何情况下都运行回调,代码总是被执行。

public function after($direction) {...}

看到参数?这可以是updown。您需要在if ($direction === 'down') { ... }之类的检查中相应地应用迁移之前或之后包装要运行的代码。

另外,我认为你的方法做得不好。如果您有大量数据,则可能会耗尽内存。

创建将添加新字段的迁移。然后是另一个迁移文件,它只进行数据转换并以块的形式处理数据。第三个将删除不再需要的字段。原因很简单:将数据与架构更改分开。