2012-05-14 46 views
7

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

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是否支持使用连接删除查询?

回答

4

您是否已使用使用Active Records

这下面query会做其他事情。

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

嘿我不想使用查询()方法。 我想用$ this-> db-> join(),$ this-> db-> delete() 明白了吗? –

4

你不能用CodeIgniter的Active Record类来做到这一点。它不支持删除查询中的连接。您必须使用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条款的插入。

+2

是的,但它应该是,希望CI的某人正在倾听 –

+2

那么为什么不把它带到他们的论坛(http://codeigniter.com/forums/)呢?或者,也许他们的错误跟踪器(https://github.com/EllisLab/CodeIgniter/issues)? –

1

我有同样的问题,该连接被忽略:

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

不是重写整个SQL的,你可以这样做:

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

通过这种方式,您可以从查询生成器(也称为Active Records)中保留好东西,并让您有自由的自定义也是。我认为,这是非常安全的,因为$sql字符串将始终以DELETE开头。当然,你可以把它作为一个快捷方式包装进函数中。