【问题标题】:How to insert value of subquery using Active Record?如何使用 Active Record 插入子查询的值?
【发布时间】:2012-11-21 01:18:02
【问题描述】:

我使用活动记录将值插入数据库。

所有其他类型的查询我都使用自定义查询,因为它更容易,但活动记录插入非常好。

所以我有这个代码:

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

现在我希望能够将 is_temp 设置为子查询结果,即:

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

如何做到这一点?

我希望避免使用第三方库。

【问题讨论】:

标签: php sql codeigniter codeigniter-2


【解决方案1】:

好吧,我怀疑你应该这样做,但 CI 不就是这样吗?

这就是我让它工作的方式(当然,从 $comment 数组中删除 is_temp):

$this->db->set($comment);
$this->db->set('is_temp',
'(SELECT allow_user_comments from subjects where id='.$subject_id.')',FALSE);
$this->db->insert('comments');  

【讨论】:

    【解决方案2】:

    请随意使用https://github.com/NTICompass/CodeIgniter-Subqueries。我已经使用它并且它有效!希望它会有用。 :-)

    【讨论】:

    • 没关系。不要忘记使用$this->db->escape($subject_id) 转义$subject_id,因为您设置了FALSE。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多