CodeIgniter Forums
DataMapper ORM - How to INSERT INTO from SELECT - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: DataMapper ORM - How to INSERT INTO from SELECT (/showthread.php?tid=55549)



DataMapper ORM - How to INSERT INTO from SELECT - El Forum - 10-31-2012

[eluser]Unknown[/eluser]
Hello,
Can somebody provide some guidance on hot to execute a query like shown here:

Code:
INSERT INTO tableA (A,B,C,D,E,F)
SELECT A,B,C,D,E
FROM tableB
WHERE a = 'Y' AND DATE( B ) = DATE(DATE(now( )) + CAST(B AS SIGNED))

I found some examples but they mention that I need to first to create a container Array, then a db->select, push query results into container array and then a db->insert, as shown below.
However that seems counterproductive or time consuming, and adds complexity to the code (at lest from my pint of view)

Code:
...
$insert =   array();
$this->db->select("")->from('');
$this->db->where(array('A' => 'Y', DATE( 'B' ) => DATE(DATE(now( )) + CAST('C' AS SIGNED))));        
$resultset = $this->db->get()->result_array();

foreach($resultset as $result){
  array_push($insert, array(
    ''     =>  $reminder[''],
    ''       =>  $reminder[''],
    ''         =>  $reminder[''],
    ...
  ));
}

$this->db->insert('', $insert);
...
Hope I'm not miss-understanding here