2011-05-03 8 views
0

두 가지 기능을 가진 "test"클래스가 있습니다. 하나는 준비된 명령문을 작성하고 하나는 하나 이상 실행하는 것입니다. 문제는 첫 번째 바인딩에서 바인딩을 설정하고 다른 함수에서 vars를 채우는 방법입니다. 당신은하지는여러 기능에서 PDO 사용 - 준비된 명령문 재사용

class test { 

    private static $moduleSql = "SELECT 
              id, 
              module 
             FROM 
              phs_pages 
             WHERE 
              prettyurl = :prettyUrl AND 
              mainId = :mainId 
              "; 

    private static function pre() { 
     $db = Db_Db::getInstance(); 
     self::$preModuleSql = $db->prepare(self::$moduleSql); 
     self::$preModuleSql->bindParam(':prettyUrl', $prettyUrl); 
     self::$preModuleSql->bindParam(':mainId', $mainId); 
    } 

    private static function getClassByDb($mainId = 0, $id = 0) { 
     $prettyUrl = self::$parts[$id]; 
     self::$preModuleSql->execute(); 
     self::$preModuleSql->debugDumpParams(); 
     $result = self::$preModuleSql->fetch(PDO::FETCH_ASSOC); 

     // get lower level classes 

     if (isset(self::$parts[$id + 1])) { 
      self::getClassByDb($result['id'], $id + 1); 
     } else { 
      return $result; 
     } 
    } 

} 

답변

1

첫 번째 함수에서 매개 변수를 바인드해야합니다, 당신은 당신이 준비된 문을 실행하기 직전, 두 번째 기능에 그렇게 할 필요가있다.

편집 : 그런데 execute을 매개 변수와 보낼 값으로 포함하는 배열로 호출 할 수도 있습니다.

+0

이 내가 할 것입니다. 그것은 아주 잘 작동합니다. 거기에 대한 욕망이 있다면, 나는 대답에 내 코드를 게시 하겠지만, 꽤 늪지 표준 PHP입니다. –

1

이 클래스 정의에는 많은 것들이 잘못되었습니다. 어.

그것은 다음과 같아야합니다

class RepositoryException extends Exception{} 

interface iRepository 
{ 
    public function store($key , PDOStatement $statement); 
    public function fetch($key); 
} 

class StatementRepository implements iRepository{ 

    protected $_pool = array(); 

    public function store($key , PDOStatement $statement) 
    { 
     $this->_pool[ $key ] = $statement; 
    } 
    public function fetch($key) 
    { 
     if (!array_key_exists($key , $this->_pool)){ 
      throw new RepositoryException( 
       "Statement with name '$key' does not exist"); 
     } 

     return $this->_pool[ $key ]; 
    } 
} 

$repo = new StatementRepository; 

$foo = new Foo($repo); 
$bar = new Bar($repo); 

$db = new PDO('sqlite::memory:'); 
$statement = $db->prepare('SELECT .... '); 

$repo->store('bljum' , $statement); 

이 방법 생성자 이름으로 iRepositoryfetch 문을 구현하는 객체를 수락 한 모든 객체입니다.

당신은 더 많은 책임을 져야합니다. 준비된 명령문을 저장하는 것이면, 매개 변수를 바인딩하고 무언가를 실행하는 것과 아무런 관련이 없어야합니다.

여기에 푸 클래스는 어떻게 할 것인지입니다 :

class Foo 
{ 
    protected $_repo = null; 

    public function __construct(iRepository $repo) 
    { 
     $this->_repo = $repo; 
    } 

    public function ergo($x , $y) 
    { 
     $statement = $this->_repo->fetch('bljum'); 
     //bind values 
     $statement->execute(); 
    } 

} 
관련 문제