简体   繁体   中英

Multiple(sharding ) table in a Model CakePHP

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM