2016-11-18 9 views
1

Codeigniter 3.0.3을 사용하고 있습니다. 쿼리 작성기로 쿼리를 작성하려고하지만 쿼리의 빌드를 여러 함수로 분할하려고합니다. 예를 들어 :Codeigniter 여러 함수로 전달되는 쿼리 빌더

class MyCart{ 

public function __construct() { 
    $this->db->select('A.id, A.title, A.price'); 
    $this->db->from('articles A'); 
} 

public function check_type($aTypeOfArticle){ 
    $this->db->where("A.type=".$aTypeOfArticle); 
} 

public function check_price($aMaxPrice){ 
    $this->db->where("A.price<=".$aMaxPrice); 
} 

public function get_result(){ 
    $query = $this->db->get(); 
    return $query->result(); 
} 

} 

하지만이 때, this->db->get() false를 반환. 이 작업을 수행 할 수 있습니까?

답변

1

이 이상 가능성이 당신을 위해 잘 작동 것,

public function query() 
{ 
    $this->db->select('A.id, A.title, A.price'); 
    $this->db->from('articles A'); 

    if(check_type($aTypeOfArticle)) 
    { 
    $this->db->where("A.type=".$aTypeOfArticle); 
    } 

    if(check_price($aMaxPrice)) 
    { 
    $this->db->where("A.price<=".$aMaxPrice); 
    } 
} 

public function check_type($aTypeOfArticle){ 
    // Do processing 
    return true or false; 
} 

public function check_price($aMaxPrice){ 
    // Do Processing 
    return true or false; 
} 

는 단일 기능에서 쿼리를 작성하고 처리 결과를 다른 함수를 호출합니다.

+0

따라서 다른 함수에서 쿼리를 분할 할 수 없습니까? 왜냐하면 나는 같은 기능에 모든 것을 확인하고 두는 많은 기준을 가지고 있기 때문에 꽤 추해 질 것이다. – ZeeCast

+0

내가 아는 것은 아니지만, 그렇지 않다는 것을 말하는 것이 아니라, 나는 어떤 식 으로든 모른다. 내가 보여준 방식이 우리가 여기에서하는 방식이라고 말할 수 있습니다. – Blinkydamo

0

Method Chaining을 살펴보십시오. from() 호출의 반환 값을 캐시 할 수 있어야합니다 (예 : 객체 속성). 그런 다음이 값을 사용하여 다른 메소드를 연결합니다.