One model, multiple tables

If I was to extend the blog demo to be multi-user, e.g. Blogger A stored their posts in table “post_A” and blogger B’s posts were stored in table “post_B”, how could I override the Post model at construction to set the tableName to A or B as necessary?

hello,

just a thought … why would you store them in separate tables? what if you’ll have 99.000 people?

I’d store them like this instead of multiple tables:




+++++++++      +++++++++++

+ users +      +  posts  +

+++++++++      +++++++++++

+ id    +---   + id      +

+ ...   +  |-->+ userID  +

+++++++++      + ...     +

               +++++++++++



–iM

imehesz is right, you most likely don’t want to do that except you really have a very good reason. It would break all rules on how to model your database tables. If you still think, it’s necessary, try to google for data model tutorials, you’ll find a lot of information out there.

OT: imehesz, how do you create your nice ASCII arts? Always wondered if there’s an editor and could find only one: JavE .

Mike:

that is super easy, just wrap your code around [ code ] [ /code ] tags :)

(without the spaces of course)

–iM

@imehesz

Thanks, i know. I meant: how do you "paint" them? Manually?

oh, I see what you mean. yes, manually (on forums).

It’s really hard to find a good DB designer, sometimes I use this one http://www.fabforce.net/dbdesigner4/

(or a drawing program, ie: google draw)

–iM

Well, I am not actually making a blog. I was just using that as an easy analogy. I will have a lot of records for each person and for performance don’t want to have to deal with database sharding of one giant table. I would prefer to simply put different tables on different servers.

Regardless of the reason, and whether or not its a good idea, is it even possible?

Check http://www.yiiframework.com/doc/api/CActiveRecord#tableName-detail

There was also a topic on sharding, if that is what you want. Try the forum search.

If you want something different, my bet is that you cause yourself a lot of headache if you try to implement such a feature with ActiveRecords as practically none of the advanced features will work. E.g. you can’t use JOINs to fetch related data if the related data is shattered across different tables.