如何在cakephp中将mysql表复制到另一个?

时间:2011-02-01 09:02:58

标签: mysql cakephp

任何人都可以指导我如何使用cakephp将一个mysql表复制到同一数据库中的另一个表中,如果可能,请给我示例查询。

2 个答案:

答案 0 :(得分:4)

create table new_table like your_table;
insert into new_table select * from your_table;

以上将保留new_table的索引

create table new_table select * from your_table;

以上不会保留索引 与cakephp无关,你只需要正确的sql语法,
加上相关的连接设置,允许php连接到mysql

答案 1 :(得分:2)

如果这是一个快速而又脏的一次性副本,我只需循环遍历源表记录并创建新的目标记录:

$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);
}

道歉,如果它是马车,我只是挥霍它。

编辑以显示行选择条件。类似的东西:

$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);
}