freddiefujiwara
freddiefujiwara

Reputation: 59029

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

Upvotes: 1

Views: 498

Answers (2)

Travis Leleu
Travis Leleu

Reputation: 4230

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.

Upvotes: 1

Nik Chankov
Nik Chankov

Reputation: 6047

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

Upvotes: 0

Related Questions