How to copy a mysql table to another in cakephp?
Can any one guide me for ho开发者_如何学Cw can i copy one mysql table to another table in same database using cakephp please give me example query if possible.
create table new_table like your_table;
insert into new_table select * from your_table;
The above will keep index for new_table
create table new_table select * from your_table;
The above will not keep index.
There is nothing to do with cakephp, you just need the correct sql syntax,
plus relevant connection setting to allow php connect to mysql
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.
Edited to show row selection conditional. Something along the lines of:
$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);
}
精彩评论