thinkphp5 支持join多表同时删除

浏览:2202 发布日期:2018/06/11 分类:ThinkPHP5专区 关键字: delete join 多表删除
//修改文件 thinkphp\library\think\db\Builder.php
//else那段实际已经多余了
//原来支持吗?
/**
     * 生成delete SQL
     * @access public
     * @param array $options 表达式
     * @return string
     */
    public function delete($options)
    {    
        //protected $deleteTablesSql    = 'DELETE %TABLE% FROM %TABLE0% %USING% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
        /*示例
        Db::table(['__ORDER__' => 'a', '__ORDER_GOODS__' => 'g'])
            ->join('__ORDER_GOODS__ g', 'a.id = g.oid', 'left')
            ->where('a.status='.$this->orderStatusNoPay.' and a.cuid='.$uid)->delete();
        */
        $table = $this->parseTable($options['table'], $options);
        $tables = explode(',', $table);
        $table = '';
        foreach($tables as $v){
            $temp = explode(' ', $v);
            if(isset($temp[1])){
                $table .= ','.$temp[1];
            }else{
                $table .= ','.$temp[0];
            }
        }
        $table = trim($table, ',');
        $sql = str_replace(
            ['%TABLE%', '%TABLE0%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
            [
                $table,
                $tables[0],
                !empty($options['using']) ? ' USING ' . $this->parseTable($options['using'], $options) . ' ' : '',
                $this->parseJoin($options['join'], $options),
                $this->parseWhere($options['where'], $options),
                $this->parseOrder($options['order'], $options),
                $this->parseLimit($options['limit']),
                $this->parseLock($options['lock']),
                $this->parseComment($options['comment']),
            ], $this->deleteTablesSql);
        return $sql;
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>