结构多个选择连接OOP类

时间:2015-05-11 11:29:11

标签: mysql oop join

你好,因为我很懒,所以我决定创建一个调用每个表的类,并在下面的类中使用它的结构

    <?php
Class dbOperations
{
    public static function select($table, $what="*", $id="", $where=""){
        if(is_array($table)){
            $query = self::selectMore($table, $what, $id, $where);
        }else{
            $query = "SELECT ".$what." FROM ".$table;
            if(!empty($id))
                $query.= " WHERE ".$id.$where;
        }
        return $result = self::send($query);
    }
    public static function exists(){

    }
    public static function insert(){

    }
    public static function update(){

    }
    public static function delete(){

    }
    public static function send($query, $param =""){
        global $dbo;
        try{
            $stmt = $dbo->prepare($query);
            /*
            * Bind the parameter if an ID was passed
            */
            if ( !empty($param) ){
                $stmt->bindParam(":id", $param, PDO::PARAM_INT);
            }
            $stmt->execute();
            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $stmt->closeCursor();
            return $results;
        }catch ( Exception $e ){
            die ( $e->getMessage() );
        }
    }
    public static function relationship(){
        //Só verifica se tem relacoes
    }
    public function selectMore($table, $select, $id="",$where){

        //SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id LEFT JOIN table3 ON table2.id=table3.id;
        $query = "SELECT ".$select;
        $select ="";
        $from ="";
        $fromAll ="";
        $whereJoin =" ON ";
        foreach($table as $key => $value){
            $whereCurJoin = $value->tname.".".$value->tid;
            //Aqui está um problema ao chamar esta funcao para 2 propósitos diferentes eles vão alterar a mesma var stati count
            $fromNow = cicleFunctions::todoCicle($table, " FROM ".$value->tname, " INNER JOIN ".$value->tname, "same");

            if(cicleFunctions::$count %2 == 1){
                $whereCurJoin.= " = ";

            }
            $fromAll .= $fromNow;
            $whereJoin .= $whereCurJoin;
            if(cicleFunctions::$count %2 == 0){
        $where =" WHERE ".$value->tname.".".$id ." = ".$where;
                $query .= $fromAll.$whereJoin.$where;
            }
        }
        var_dump($query);
        return $query;
    }
}

要调用此类我使用此

$result = dbOperations::select($DB->tables->produtos->tname, $DB->tables->produtos->columns->nome.", ".$DB->tables->produtos->columns->preco, $DB->tables->produtos->columns->preco, " >10");

这个工作正常,即使我必须使用大量的引用只是为了放置我想要的表的名称$DB->tables->produtos->tname 我真正的问题是在方法selectMore我想允许自己使用多个连接,到目前为止我只设法使用1个连接工作正常我认为我的问题是strutsuring这个类应该收到的数组 请不要谈论MVC,我知道它是什么,我只是很开心,谢谢

0 个答案:

没有答案