4
votes

I have a news tracking website, that we have migrating over to Codeigniter, i have run in to an issue with using Codeigniter Active Record - I do not have enough knowledge to figure this one out.

In the old code - we do an insert and within the insert we do a SELECT FROM to get the value for one field in the insert

$this->db->query("INSERT INTO news_item_keywords_link (news_item_keyword, keyword) 
SELECT $version_id, keyword FROM news_item_keywords_link WHERE       news_item_keyword=$old_version

I have tried to do this using the Active Record and I can not figure out how to do it or it maybe that you can not do it with Active Record.

I have tried a few things, my question how do you do the inline FROM in an INSERT statement with Active Record. I tried this way, and it did not work get syntax error, any suggestions.

if ($this->db->insert('news_item_keywords', Array(
                    'news_item_keyword' => $version_id,
                    'keyword' => $this-db->select('keyword') 
                                 ->from('news_item_keywords')
                                 ->where('news_item_keyword', $old_version);
    ))) {

Should I install an ORM library to extend the functionality? If so what ORM library is recommended?

1
is there a problem with the standard sql query? it is much better, active record is just a query builder, the outcome is just the sametomexsans
your code is confusingDenmark

1 Answers

0
votes

1st thing load the model in the controller...

Model:

function add($data){
$this->insert("tablename",$data);
}
function select_keyword($oldversion){
 $this->db->select("keyword");
 $this->db->where("news_item_keywords",$oldversion);
 $this->db->get("news_item_keywords");
}

Controller:

function create(){

$data = array('news_item_keyword' => $version_id,
              'news_item_keywords' => $this->modelname->select_keyword("oldversion")
             );
$this->modelname->insert($data);//successful!

}