1

i use Active record for inserting values to DB.

All other type of queries i do with custom query as it's much easier but the active record insert is quite nice.

So i have this code :

    $comment = array (
'point_id' => $object->id,
'title' => $object->title,
'comment' => $object->comment,
'author_name' => $object->author_name,
'is_temp' => 0,
'parent_id' => $object->parent_id

);
return $this->db->insert('comments', $comment);

Now i want to be able to set is_temp as a subquery result, which is :

(SELECT allow_user_comments from subjects where id='somevalue')

How would one achive that?

I was hoping to avoid using third party libraries.

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • just a note. Codeigniter does not have active record. It has only query builder. – itachi Dec 03 '12 at 14:55
  • @itachi incorrect. See the first paragraph of this page - http://ellislab.com/codeigniter/user-guide/database/active_record.html – Catfish Dec 03 '12 at 14:58
  • Related, may be useful to you - http://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record – Shauna Dec 03 '12 at 15:00
  • @Catfish That's a misleading information from the developers. Codeigniter AR doesn't have the basic ground of Proper AR and is nothing but query builder(just open it up and you'l see). – itachi Dec 03 '12 at 15:02
  • Can you post the sql of the entire query that you are trying to achieve? – Catfish Dec 03 '12 at 15:05
  • It was just a basic insert, i don't see how's the helping but thank you Catfish i've solved the problem. – eric.itzhak Dec 03 '12 at 15:05

2 Answers2

2

Well, i doubt the fact that that's how you're supposed to do it, but ain't CI all about that?

This is how i got it to work (removing is_temp from the $comment array ofcourse):

$this->db->set($comment);
$this->db->set('is_temp',
'(SELECT allow_user_comments from subjects where id='.$subject_id.')',FALSE);
$this->db->insert('comments');  
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
0

Feel free to use https://github.com/NTICompass/CodeIgniter-Subqueries. I have used it and it works! Hope it would be useful. :-)

AMYunus
  • 392
  • 1
  • 7
  • Nevermind. Do not forget to escape the `$subject_id` with `$this->db->escape($subject_id)` since you set it `FALSE`. Good luck! – AMYunus Dec 05 '12 at 03:53