数组值未传递到set方法值

时间:2016-08-27 06:13:42

标签: php arrays query-builder

我正在尝试组建一个数据库查询构造类,它将为预准备语句构造字符串。

function SetOperVals($cols,$oper) {
    foreach(array_combine($cols,$oper) as $c => $o) {
        $cv[$i] = $c.$o.'?';
        $i++;
    }
    $this->OperVals = $cv;
}

我可以在这个函数中使用var_dump($ cv),我看到了我期待的输出,但是当我使用下面的方法获取值时,它返回null:

function GetOperVals() {
    return $this->OperVals;
}

我很肯定它有一些非常明显的东西,因为它已经很晚了,我已经累了...... ......任何帮助都会受到赞赏 - 在此先感谢!

//根据要求 - 下面的完整课程以及如何调用它:

class sql {

    // need some conditions in here to determine how to setup the string
    function ConstructPrepared() {
            return      $this->GetQueryType().' '
                    .   $this->GetColumns().' '
                    .   $this->GetAction().' '
                    .   $this->GetSchema().''
                    .   $this->GetTable().' '
                    .   $this->GetConditions()
                    ;
    }
    function GetConditions() {
        return $this->Conditions;
    }
    function GetConditionsAndValues(){
        return $this->ConditionsAndValues;
    }
    function GetOperVals() {
        return $this->OperVals;
    }
    function GetQueryType() {
        return $this->Type;
    }
    function GetAction(){
        return $this->Action;
    }
    function GetSchema(){
        return $this->Schema;
    }
    function GetTable(){
        return $this->Table;
    }
    function GetColumns(){
        return $this->Cols;
    }
    function GetVals(){
        return $this->Vals;
    }
    function GetColsandVals(){
        return $this->ColsVals;
    }

    function WrapString($s) {
        if( gettype($s) == string) {
            return '\''.$s.'\'';
    } else { return $s; }
    }    

    function SetOperVals($cols,$oper) {
        $i = 0;
            foreach(array_combine($cols,$oper) as $c => $o) {
                $cv[$i] = $c.$o.'?';
                $i++;
                }
        $this->OperVals = $cv;
    }

    function SetQueryType($type) {
            $this->SetAction($type);
            switch(strtolower($type)) {
            case 's' : case 'select' : $this->Type = 'select'; break;
            case 'i' : case 'insert' : $this->Type = 'insert'; break;
            case 'u' : case 'update' : $this->Type = 'update'; break;
            case 'd' : case 'delete' : $this->Type = 'delete'; break;
        }
    }

    function SetAction($action) {
            switch(strtolower($action)) {
            case 's' : case 'select' : $this->Action =' from '; break;
            case 'i' : case 'insert' : $this->Action = ' into '; break;
            case 'u' : case 'update' : $this->Action = ''; break;
            case 'd' : case 'delete' : $this->Action = ' from '; break;
            }
    }

    function SetTable($table) {
        $this->Table = $table;
    }

    function SetSchema($schema) {
        if($schema) {
            $this->Schema = $schema.'.';
        } else $this->Schema = null;
    }

    function SetColumns($columns, $oper) {
        switch($this->GetQueryType()) {
            case 'select' : $this->Cols = implode(',',$columns); break;
            case 'insert' : $this->Cols = implode(',',$columns); break;
            case 'delete' : $this->Cols = null; break;
            case 'update' : $this->Cols = SetColsandVals($columns , GetVals(), $oper);
        }
    }

    function SetVals($values) {
        switch(GetType()) {
            case 's' : $this->Vals = implode(',',$values); break;
            case 'i' : $this->Vals = implode(',',$values); break;
            case 'd' : $this->Vals = null; break;
            case 'u' : $this->Vals = $values; // pass them on to the ColsandVals function in the SetColumns function, do nothing here
        }
    }    

    function SetColsandVals($cols,$oper) {
        $this->ColsVals = GetOperVals($cols,$oper);
    }

    function SetConditionsAndValues($cols,$oper) {
//                var_dump($cols,$oper);
        $this->ConditionsAndValues = $this->GetOperVals($cols,$oper);
    }

    function SetConditions($cols,$vals,$oper) {
                var_dump($cols,$oper);
        if($cols) {
            $this->Conditions = 'where '.$this->GetConditionsAndValues($cols,$oper);
        }
    }

}

以下是它在index.php测试文件中的调用方式:

$q = new sql;
$q->SetQueryType('s');
$q->SetTable('test_table');
$q->SetColumns(array('id','name'), $oper);
$q->SetConditionsAndValues(
            $cols = array('ID')
        ,   $vals = array(1)
        ,   $ops = array('=')
        );
$q->SetOperVals($cols,$ops);

echo $q->ConstructPrepared();

0 个答案:

没有答案