Is it possible to UPDATE a JOINed table using Codeigniter's Active Record?

梦想与她 提交于 2019-12-07 08:39:29

问题


Here is what I'd like to do

function edit_save($data, $post_id, $user_id)
{
    $this->db->where('post.user_id', $user_id);
    $this->db->where('post.post_id', $post_id);
    $this->db->join('data', 'post.data_id_fk = data.data_id', 'left');
    $this->db->update('post', $data);
}

The 'post' table needs to be left-joined with 'data'.

When I run the above I get a SQL error saying that one of the fields from the 'data' table is not found.

Any suggestions?

MORE INFO

This is the generated SQL query

UPDATE `post` 
SET `data_value` = '111', `data_date` = '2012-02-13', `post_text` = '111' 
WHERE `post_stream_id` =  '5' 
    AND `post_id` =  '18'

This is the error

Unknown column 'data_value' in 'field list'

It doesn't show the JOIN statement.


回答1:


Try this active record query for update with joins:

function edit_save($data, $post_id, $user_id)
{
    $this->db->set($data)
    $this->db->where('post.user_id', $user_id);
    $this->db->where('post.post_id', $post_id);
    $this->db->where('post.data_id_fk = data.data_id');
    $this->db->update('post, data');
}


来源:https://stackoverflow.com/questions/9254166/is-it-possible-to-update-a-joined-table-using-codeigniters-active-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!