如何在cakephp中将一个mysql表复制到另一个表? [英] How to copy a mysql table to another in cakephp?
本文介绍了如何在cakephp中将一个mysql表复制到另一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以指导我如何使用cakephp将一个mysql表复制到同一数据库中的另一个表吗?如果可能,请给我示例查询.
Can any one guide me for how can i copy one mysql table to another table in same database using cakephp please give me example query if possible.
推荐答案
如果是一次快速且肮脏的副本,我将遍历源表记录并创建新的目标记录:
If it's a quick and dirty one time copy, I'd just loop over the source table records and create new target records:
$source = $this->Source->find('all');
foreach($source as $sRec)
{
$this->Target->create();
$targetData = array();
$target['Target']['field1'] = $sRec['Source']['field1'];
$target['Target']['field2'] = $sRec['Source']['field2'];
//etc
$this->Target->save($targetData);
}
很抱歉,如果是越野车,我就把它洒了.
Apologies if it's buggy, I've just splurged it.
已编辑以显示有条件的行选择.类似于:
$toCopy = array(1,32,71,72,73);
foreach($toCopy as $anId)
{
$sRec = $this->Source->read(null,$anId);
$this->Target->create();
$targetData = array();
$target['Target']['field1'] = $sRec['Source']['field1'];
$target['Target']['field2'] = $sRec['Source']['field2'];
//etc
$this->Target->save($targetData);
}
这篇关于如何在cakephp中将一个mysql表复制到另一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文