Codeigniter使用联接表删除数据

Codeigniter使用联接表删除数据,codeigniter,join,activerecord,sql-delete,Codeigniter,Join,Activerecord,Sql Delete,从逻辑上讲,在SQL中,我们可以使用连接从表中删除数据,例如 DELETE clb_subscriber_group_mapping .* FROM clb_subscriber_group_mapping INNER JOIN clb_driver_group ON (clb_driver_group.id = clb_subscriber_group_mapping.driver_group_id) INNER JOIN clb_company ON (clb_company.id =

从逻辑上讲,在SQL中,我们可以使用连接从表中删除数据,例如

DELETE  clb_subscriber_group_mapping .* FROM clb_subscriber_group_mapping 
INNER JOIN clb_driver_group ON (clb_driver_group.id = clb_subscriber_group_mapping.driver_group_id)
INNER JOIN clb_company ON (clb_company.id = clb_driver_group.company_id)
WHERE clb_company.id = 256 AND clb_subscriber_group_mapping.subscriber_id = 1784;
CodeIgniter与上述查询的等价物是什么

CodeIgniter是否支持带联接的删除查询?

是否必须使用

下面这一点就可以了

$int_company_id = 256;
$int_subscriber_id = 1784;

$this->db->query("
DELETE  clb_subscriber_group_mapping .* FROM clb_subscriber_group_mapping 
INNER JOIN clb_driver_group ON (clb_driver_group.id = clb_subscriber_group_mapping.driver_group_id)
INNER JOIN clb_company ON (clb_company.id = clb_driver_group.company_id)
WHERE clb_company.id = ? AND clb_subscriber_group_mapping.subscriber_id = ?;

", array($int_company_id, $int_subscriber_id));

你不能用CodeIgniter的活动记录类来实现这一点。它不支持删除查询中的联接。您必须使用Robin Castlin提到的
$this->db->query()
执行查询

下面的代码取自核心文件。它是生成
DELETE
查询的内部组件之一

function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
    $conditions = '';

    if (count($where) > 0 OR count($like) > 0)
    {
        $conditions = "\nWHERE ";
        $conditions .= implode("\n", $this->ar_where);

        if (count($where) > 0 && count($like) > 0)
        {
            $conditions .= " AND ";
        }
        $conditions .= implode("\n", $like);
    }

    $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;

    return "DELETE FROM ".$table.$conditions.$limit;
}

如您所见,其中没有任何内容指定插入
JOIN
子句。

我遇到了相同的问题,忽略了JOIN:

$r1 = $db->where("object2_type", $object_type)
->join($object_type, "$object_type.id = object1_id", "LEFT")
->where("$object_type.id IS NULL", null, false)
->delete("knots");
所以我是这样做的:

$ids = $db->select("knots.id")
->where("object2_type", $object_type)
->join($object_type, "$object_type.id = object1_id", "LEFT")
->where("$object_type.id IS NULL", null, false)
->get("knots")->result_object();
/* my ow function which generates a string like '1','2','3' or 0 */    
$ids_csv = $this->mh()->get_flat_items_as_csv($ids);
$r = $db->where("knots.id IN ($ids_csv)", null, false)->delete("knots);
// build query as usual...
$this->db
    ->from ('cookies')
    ->join ('recipes', 'recipes.cookie = cookies.id')
    ->where ('recipes.rating', 'BAD');

/* 
 * get the original DELETE SQL, in our case:
 * "DELETE FROM `cookies` JOIN `recipes` ON `recipes`.`cookie`=`cookies`.`id` WHERE `recipes`.`rating`='BAD'"
 */
$sql = $this->db->get_compiled_delete ('cookies');

/*
 * insert the target table in the SQL string, to get this:
 * "DELETE `cookies` FROM `cookies` JOIN `recipes` ON `recipes`.`cookie`=`cookies`.`id` WHERE `recipes`.`rating`='BAD'"
 */
$target = $this->db->escape_identifiers ('cookies'); // just to be safe
$sql = substr_replace ($sql, " $target", 6, 0); // note the prepended space

// now we can run the query
$q = $this->db->query ($sql);

您可以这样做,而不是重写整个SQL:

$ids = $db->select("knots.id")
->where("object2_type", $object_type)
->join($object_type, "$object_type.id = object1_id", "LEFT")
->where("$object_type.id IS NULL", null, false)
->get("knots")->result_object();
/* my ow function which generates a string like '1','2','3' or 0 */    
$ids_csv = $this->mh()->get_flat_items_as_csv($ids);
$r = $db->where("knots.id IN ($ids_csv)", null, false)->delete("knots);
// build query as usual...
$this->db
    ->from ('cookies')
    ->join ('recipes', 'recipes.cookie = cookies.id')
    ->where ('recipes.rating', 'BAD');

/* 
 * get the original DELETE SQL, in our case:
 * "DELETE FROM `cookies` JOIN `recipes` ON `recipes`.`cookie`=`cookies`.`id` WHERE `recipes`.`rating`='BAD'"
 */
$sql = $this->db->get_compiled_delete ('cookies');

/*
 * insert the target table in the SQL string, to get this:
 * "DELETE `cookies` FROM `cookies` JOIN `recipes` ON `recipes`.`cookie`=`cookies`.`id` WHERE `recipes`.`rating`='BAD'"
 */
$target = $this->db->escape_identifiers ('cookies'); // just to be safe
$sql = substr_replace ($sql, " $target", 6, 0); // note the prepended space

// now we can run the query
$q = $this->db->query ($sql);

这样,您就可以从查询生成器(又称活动记录)中保留好的内容,并可以自由定制。我认为这是非常安全的,因为
$sql
字符串总是以
DELETE
开头。当然,您可以将其作为快捷方式包装到函数中。

嘿,我不想使用query()方法。我想用$this->db->join(),$this->db->delete()明白我的意思了吗?是的,但应该是,希望CI的人在听。那么你为什么不把它带到他们的论坛()呢?或者,也许是他们的bug追踪器()?