Cakephp 迁移:重命名字段并更新数据库中的值
Cakephp migrations: Rename fields and update values in database
我正在尝试重命名字段并更新 cakephp 迁移中已插入行的值。
工作流程:
在之前的方法中,我获取了我的数据库中所有已添加的行,因此我将能够在之后的方法中更新它们。
然后发生迁移,创建列 value_from 和 value_to 并删除列 price_from 和 price_to
然后我尝试在我的 after 方法中获取所有 "new" 行,我不想从旧行更新值,但是错误发生了,因为 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'
我怎样才能克服我的问题?
如果您需要任何其他信息,请告诉我,我会提供。提前致谢!
你运行回调在任何情况下,代码总是被执行。
public function after($direction) {...}
看到论点了吗?这可以是 up
和 down
。您需要在 if ($direction === 'down') { ... }
.
之类的检查中相应地在应用迁移之前或之后包装您想要 运行 的代码
另外,我认为您的方法做得不好。如果您有大量数据,您可能 运行 内存不足。
创建将添加新字段的迁移。然后是另一个迁移文件,它将只进行数据转换并以块的形式处理数据。第三个将删除之后不再需要的字段。原因很简单:将数据与架构更改分开。
我正在尝试重命名字段并更新 cakephp 迁移中已插入行的值。
工作流程:
在之前的方法中,我获取了我的数据库中所有已添加的行,因此我将能够在之后的方法中更新它们。
然后发生迁移,创建列 value_from 和 value_to 并删除列 price_from 和 price_to
然后我尝试在我的 after 方法中获取所有 "new" 行,我不想从旧行更新值,但是错误发生了,因为 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'
我怎样才能克服我的问题? 如果您需要任何其他信息,请告诉我,我会提供。提前致谢!
你运行回调在任何情况下,代码总是被执行。
public function after($direction) {...}
看到论点了吗?这可以是 up
和 down
。您需要在 if ($direction === 'down') { ... }
.
另外,我认为您的方法做得不好。如果您有大量数据,您可能 运行 内存不足。
创建将添加新字段的迁移。然后是另一个迁移文件,它将只进行数据转换并以块的形式处理数据。第三个将删除之后不再需要的字段。原因很简单:将数据与架构更改分开。