开发者

Multiple(sharding ) table in a Model CakePHP

开发者 https://www.devze.com 2023-01-26 01:25 出处:网络
There are many tables as the following: table_2010 table_2009开发者_运维知识库 table_2008 table_2007

There are many tables as the following:

 table_2010
 table_2009开发者_运维知识库
 table_2008
 table_2007
     .
     .

Using MySQL 4 + PHP5 + CakePHP 1.3

My Question is

How to treat these tables in a model? I wanna treat like this

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


I agree with Nik -- unless you're sharding for performance reasons, I would combine all of your tables into one table, with a column for the year (if you make it an INT, it won't affect performance much).

However, if you need to shard your tables, I'd recommend that you just override the Model::find() method to accept additional parameters. In your model, write something like the pseudocode below:

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 );
}

Basically the call to setSource will change your table that you are querying, at runtime. See Can a CakePHP model change its table without being re-instantiated? for more information.


For me the smarter way is to use one table - posts for example and in that table to have a special column called year. So the find will be something like:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消