|
Joomla! / 開發技術 / Eddy Chang / 週四, 15 七月 2010 20:06 |
因為JTable通常只適合在單一個資料表中運作,如果有多個資料表互相會參照欄位的情況,例如有JOIN的情況發生,在定義JTable子類別時,就需要覆蓋(Override)原有的store或delete方法,以下是一個可執行的範例,不過這樣作似乎也沒用到JTable的好處…:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
class UTable extends JTable {
var $_tbl_key2 = '';
function __construct( $table, $key, $key2, &$db ) {
$this->_tbl = $table;
$this->_tbl_key = $key;
$this->_tbl_key2 = $key2;
$this->_db =& $db;
}
function getKey2Name() {
return $this->_tbl_key2;
}
function store2( $nid=null, $nid2=null ) {
$k = $this->_tbl_key;
$k2 = $this->_tbl_key2;
if ($nid) {$this->$k = intval( $nid );}
if ($nid2) {$this->$k2 = intval( $nid2 );}
if( $this->$k && $this->$k2) {
$query = 'SELECT * FROM '.$this->_db->nameQuote( $this->_tbl ).' WHERE '.$this->_tbl_key.'='.$this->_db->Quote($this->$k).
' AND '.$this->_tbl2_key.'='.$this->_db->Quote($this->$k2);
$this->_db->setQuery( $query );
if ($this->_db->query()) {exit('already stored'); return true;} //Already stored
else {
$query = 'INSERT INTO '.$this->_db->nameQuote( $this->_tbl ).' ('.$this->_tbl_key.', '.$this->_tbl_key2.')'.
' VALUES ('.$this->_db->Quote($this->$k).', '.$this->_db->Quote($this->$k2).')';
$this->_db->setQuery( $query );
if ($this->_db->query()) {return true;}
else {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
}
else {return false;} //ID's not provided
}
function delete2( $oid=null, $oid2=null )
{
$k = $this->_tbl_key;
if ($oid) {$this->$k = intval( $oid );}
$k2 = $this->_tbl_key2;
if ($oid2) {$this->$k2 = intval( $oid2 );}
$query = 'DELETE FROM '.$this->_db->nameQuote( $this->_tbl ).
' WHERE '.$this->_tbl_key.' = '. $this->_db->Quote($this->$k).
' AND '.$this->_tbl_key2.' = '. $this->_db->Quote($this->$k2);
$this->_db->setQuery( $query );
if ($this->_db->query())
{return true;}
else {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
}
?>
|
評論: |