2010-11-22 42 views
1

有多少表如下:多(分片)表模型CakePHP的

table_2010 
table_2009 
table_2008 
table_2007 
    . 
    . 

使用MySQL 4 + PHP5 + CakePHP的1.3

我的問題是

如何對待這些模型中的表格? 我要像對待這個

Table->find('all',"2010",array("conditions"=>"")); 
+2

這可能會幫助你http://stackoverflow.com/questions/2710860/problem-with-altering-model-attributes-in-controller – Young 2010-11-22 06:59:10

回答

1

我同意Nik的觀點 - 除非你因性能原因而分片,否則我會將所有的表格合併到一張表格中,並附上一年的專欄(如果您將其設置爲INT,則不會影響性能許多)。然而,如果你需要分割你的表,我建議你重寫Model :: find()方法來接受其他參數。在模型中,寫的東西像下面的僞代碼:

function find($type, $options = array()) { 
if(isset($options['table'])) { // this is the index where you'll pass your table name 
    $this->setSource($options['table']; 
} 

return parent::find($type, $options); 
} 

基本上調用的SetSource會改變你的表,你是查詢,在運行時。有關更多信息,請參閱Can a CakePHP model change its table without being re-instantiated?

0

對我來說更聰明的方法是使用一個表 - 職位例子,該表中有一個名爲一年專欄。所以發現將會是這樣的:

$ this-> Post-> find('all',array('year'=> 2010));