src/MVC/DataBase/Model.php
protected function select(array $fields, array $criteria = array(), array $operators = array(), array $conditions = array())
    {
        $sql = "";
        $stringConditions = array();
        
        if ((!empty($criteria) && !empty($operators)) && !empty($conditions)) {
            if ((count($criteria) == count($operators) && (count($criteria) - 1) == count($conditions)) 
                && ((count($operators) -1) != count($conditions) && count($criteria) > 1)) {
                throw new \Exception("The number of Criteria, Operators and Conditions are not equals.");
            }
            
            $criteriaFields = array_keys($criteria);
            
            $stringConditions[] = "$criteriaFields[0] $operators[0] ?";
            
            for ($i = 1; $i < count($criteriaFields); $i++) {
                $j = $i - 1;
                $stringConditions[] = "$conditions[$j] $criteriaFields[$i] $operators[$i] ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' ', $stringConditions);
        } elseif ((!empty($criteria) && !empty($operators)) && count($criteria) > 1) {
            if (count($criteria) != count($operators)) {
                throw new \Exception("The number of Criteria and Operators are not equals.");
            }
            
            $criteriaFields = array_keys($criteria);
            
            for ($i = 0; $i < count($criteriaFields); $i++) {
                $stringConditions[] = "$criteriaFields[$i] $operators[$i] ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' AND ', $stringConditions);
        } elseif (!empty($criteria)) {
            foreach ($criteria as $field => $value) {
                $stringConditions[] = "$field = ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' AND ', $stringConditions);
        } elseif (!empty($fields)) {
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table";
        }
        
        $pdos = $this->_pdo->prepare($sql);
        $pdos->execute(array_values($criteria));
        
        return $pdos->fetchAll();
    }
protected function select(array $fields, array $criteria = array(), array $operators = array(), array $conditions = array())
    {
        $sql = "";
        $stringConditions = array();
        
        if ((!empty($criteria) && !empty($operators)) && !empty($conditions)) {
            if ((count($criteria) == count($operators) && (count($criteria) - 1) == count($conditions)) 
                && ((count($operators) -1) != count($conditions) && count($criteria) > 1)) {
                throw new \Exception("The number of Criteria, Operators and Conditions are not equals.");
            }
            
            $criteriaFields = array_keys($criteria);
            
            $stringConditions[] = "$criteriaFields[0] $operators[0] ?";
            
            for ($i = 1; $i < count($criteriaFields); $i++) {
                $j = $i - 1;
                $stringConditions[] = "$conditions[$j] $criteriaFields[$i] $operators[$i] ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' ', $stringConditions);
        } elseif ((!empty($criteria) && !empty($operators)) && count($criteria) > 1) {
            if (count($criteria) != count($operators)) {
                throw new \Exception("The number of Criteria and Operators are not equals.");
            }
            
            $criteriaFields = array_keys($criteria);
            
            for ($i = 0; $i < count($criteriaFields); $i++) {
                $stringConditions[] = "$criteriaFields[$i] $operators[$i] ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' AND ', $stringConditions);
        } elseif (!empty($criteria)) {
            foreach ($criteria as $field => $value) {
                $stringConditions[] = "$field = ?";
            }
            
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table " 
                . "WHERE " . implode(' AND ', $stringConditions);
        } elseif (!empty($fields)) {
            $sql = "SELECT " . implode(', ', $fields) . " FROM $this->_table";
        }
        
        $pdos = $this->_pdo->prepare($sql);
        $pdos->execute(array_values($criteria));
        
        return $pdos->fetchAll();
    }
protected function select ( array $ fields , array $ criteria = array ( ) , array $ operators = array ( ) , array $ conditions = array ( ) ) { $ sql = "" ; $ stringConditions = array ( ) ; if ( ( ! empty ( $ criteria ) && ! empty ( $ operators ) ) && ! empty ( $ conditions ) ) { if ( ( count ( $ criteria ) == count ( $ operators ) && ( count ( $ criteria ) - 1 ) == count ( $ conditions ) ) && ( ( count ( $ operators ) - 1 ) != count ( $ conditions ) && count ( $ criteria ) > 1 ) ) { throw new \ Exception ( "The number of Criteria, Operators and Conditions are not equals." ) ; } $ criteriaFields = array_keys ( $ criteria ) ; $ stringConditions [ ] = "$criteriaFields[0] $operators[0] ?" ; for ( $ i = 1 ; $ i < count ( $ criteriaFields ) ; $ i ++ ) { $ j = $ i - 1 ; $ stringConditions [ ] = "$conditions[$j] $criteriaFields[$i] $operators[$i] ?" ; } $ sql = "SELECT " . implode ( ', ' , $ fields ) . " FROM $this->_table " . "WHERE " . implode ( ' ' , $ stringConditions ) ; } elseif ( ( ! empty ( $ criteria ) && ! empty ( $ operators ) ) && count ( $ criteria ) > 1 ) { if ( count ( $ criteria ) != count ( $ operators ) ) { throw new \ Exception ( "The number of Criteria and Operators are not equals." ) ; } $ criteriaFields = array_keys ( $ criteria ) ; for ( $ i = 0 ; $ i < count ( $ criteriaFields ) ; $ i ++ ) { $ stringConditions [ ] = "$criteriaFields[$i] $operators[$i] ?" ; } $ sql = "SELECT " . implode ( ', ' , $ fields ) . " FROM $this->_table " . "WHERE " . implode ( ' AND ' , $ stringConditions ) ; } elseif ( ! empty ( $ criteria ) ) { foreach ( $ criteria as $ field => $ value ) { $ stringConditions [ ] = "$field = ?" ; } $ sql = "SELECT " . implode ( ', ' , $ fields ) . " FROM $this->_table " . "WHERE " . implode ( ' AND ' , $ stringConditions ) ; } elseif ( ! empty ( $ fields ) ) { $ sql = "SELECT " . implode ( ', ' , $ fields ) . " FROM $this->_table" ; } $ pdos = $ this -> _pdo -> prepare ( $ sql ) ; $ pdos -> execute ( array_values ( $ criteria ) ) ; return $ pdos -> fetchAll ( ) ; }
Function SQL SELECT
@access protected
@param array $fields
@param array $criteria
@param array $operators
@param array $conditions
@return array
@throws \Exception
Function SQL SELECT