2014-11-02 60 views
-1
$db->select("users")->where(array("username", "=", "username")); 
$db->update("users", array("username" => "username", "password" => "12345"))->where(array("id", "=", "14")); 

好吧,我想通過將where()方法鏈接到select,update和delete來編寫類似上面的語句。 我的問題是;如何確定在where之前是否使用了select,update或delete,所以我可以將正確的值綁定到正確的語句上。PHP中的鏈接方法

我想是這樣的:

public function where() { 
    if($this->select()) { 
     // so if $db->select("users")->where(array("username", "=", "username")); 
     // save the where data in the select variable. 
    } 
    elseif($this->update()) { 
     // so if $db->update("users", array("username" => "username", "password" => "12345"))->where(array("id", "=", "14")); 
     // save the where data in the update variable. 
    } 
    elseif($this->delete()) { 
     // so if $db->delete("users")->where(array("username", "=", "username")); 
     // save the where data in the delete variable. 
    } 
} 

但上面的代碼當然是無效的,而且我不使用任何框架。

public function select($table, $what = null) { 
    $what == null ? $what = "*" : $what; 

    $this->_select = "SELECT {$what} FROM {$table}"; 
     return $this; 
} 
+0

我d建議在'select','update'和'delete'方法中設置標誌。 – hjpotter92 2014-11-02 19:34:52

回答

2

您將不得不維持該狀態。這不是要告訴以前的電話是select()還是update(),這是考慮問題的錯誤方法。您只需要每個select/update/delete修改$this,以便$this,總是知道它正在建設什麼樣的查詢。

一個死簡單的例子:

public function select() { 
    $this->kind == 'select'; 
    return $this; 
} 

public function where() { 
    if ($this->kind == 'select') { 
    ... 
    return $this; 
} 

,你的鏈接方法份額,他們都返回$this,使後續的方法可以鏈接到最終的唯一的事。這完全是關於在$this中存儲狀態,直到某個最終方法調用實際上隱藏了構建的查詢。

+0

謝謝你的時間! – jeastogg 2014-11-02 20:07:46

1

喜歡的東西:

public function select($table, $fields = '*') 
{ 
    $this->query = "SELECT {$fields} FROM `{$table}`"; 
    return $this; 
} 

public function where($conditions = []) 
{ 
    if ($this->query) 
    { 
     if ($conditions) 
     { 
      foreach ($conditions as $key => &$val) 
       $val = "`{$key}` = '{$val}'"; 
      $this->query .= ' WHERE ' . implode(' AND ', $conditions); 
     } 
     $db->query($this->query); 
     $this->query = ''; 
     return $this; 
    } 
} 

這將工作,但是,你必須注意到,這種結構將允許你做這樣的事情:

$db->where(); 

這是完全合法的,即使沒有按」不要直接在數據庫中調用where()

另外,不需要WHERE子句的查詢將不會運行,因爲只有where()實際進行呼叫。

如何解決這個問題?

我們實際上可以使用一個非常有趣的OOP機制:析構函數方法。 PHP在不再使用時立即銷燬對象,我們可以在這裏探索此功能作爲運行查詢的觸發器。我們只需將查詢分隔到一個新的對象。

class dbQuery 
{ 
    private $db; 
    private $conditions = []; 

    function where($conditions = []) 
    { 
     $this->conditions = array_merge($this->conditions, $conditions); 
     return $this; 
    } 

    function __construct($db, $query) 
    { 
     $this->db = $db; 
     $this->query = $query; 
    } 

    function __destruct() 
    { 
     if ($this->conditions) 
     { 
      foreach ($this->conditions as $key => &$val) 
       $val = "`{$key}` = '{$val}'"; 
      $this->query .= ' WHERE ' . implode(' AND ', $this->conditions); 
     } 
     $this->db->result = $db->query($this->query); 
    } 
} 

class Database 
{ 
    public $result = null; 
    protected static $instance; 
    function __construct() 
    { 
     if (!self::$instance) 
      self::$instance = new mysqli('localhost', 'user', 'password', 'dbname'); 
    } 
    public function query($query) 
    { 
     return self::$instance->query($query); 
    } 
    public function select($table, $fields = '*') 
    { 
     return new dbQuery($this, "SELECT {$fields} FROM `{$table}`"); 
    } 
} 

這種方式它不存在$db->where()將無法​​正常工作,並使用$db->select('table')$db->select('table')->where([...])都將得出的結果,甚至可以延伸到使用where()多次類似的語法:

$db->select('table')->where(['id' => 100])->where(['price' => 1.99]);