Multiple db table using one AR


i have 24 tables that share the same columns(meta data) in them so i decided to have one AR class that will help in manipulating records of these tables.

class Subject extends CActiveRecord{

private $_subject; 

public function __construct($subject){

  $this->_subject= $subject;


public function tableName(){

return $this->_subject;

i get to realize that the __construct function doesnt work b’cos when i call this class

like this

 $model = new Subject('english');

i receive an error

"The table "" for active record class "Subject" cannot be found in the database."

meaning the table is empty.

any idea on these problem???? :o :o :o

Try to call the parent constructor after you reload it:

public function __construct($subject){

  $this->_subject= $subject;

  return parent::__construct();


Oppssss, :( :(

this didn’t work echo an error

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in C:\xampp\xampp\htdocs\cmf\yii\framework\db\ar\CActiveRecord.php on line 22

Strange, 134 Mb ???

Ok, add much more memory in php.ini:

memory_limit = 256M

i dont think it will work that way cos i changed the memory_limit to 512Mb and still gave the same error :-[

I really dont see the decision. Even if you somehow pass the dynamic table name to the constructor, how do you pass the name when you call the static methods? I mean constructions like:


May I ask what for do you have 24 tables with the same columns?

the 24 tables are for different subjects.(english, mathematics , physics,chemistry…)

all have the same table structure.

If you’re running out of memory like that then in all likelihood you’re entering an infinite loop somehow, although I’m perplexed how setting the table name in a deferred manner would cause that.

My two suggestions would be:


[*]You could extend your Subject class, creating 24 more classes, but only overriding the tableName function in each one. Thus, your core logic is shared. They really can be as simple as:

class Maths extends Subject


	public function tableName()


		return 'Maths';



[*]Is there any reason you couldn’t move all 24 tables into one, with a separate Subject field to define which row is of which type? Although the extending method should work, it creates a bunch of classes. Having all of your data in the same table also simplifies things like searching for information in said database. If each item is a class, for example, with figures such as lesson length and class size, it’d be much easier to query which class size is the smallest or view a list of classes ordered by lesson length.


It’s better to merge your tables, it’s really not expensive. You have serious reasons to hold 'em separately?

@ Lilt Excellent!!! :D

i think this is an immediate solution. :)

Just creating extra classes that will extend the subject class is the best approach

thanks to all

is it possible to merge to one table that will holds 3 different semesters result of a year for each student that offers 12 different subjects and each subject will have 4 column of assessment and a column for exam???

and each class(group of students that are in one course eg electrical eng) will have a way to calculate a highest/lowest per subject in that class??

and the first position (based on the highest total scores ) to the last still based on class will be found.

and so many other manipulations… :mellow: :mellow:

i will be glad if u can figure it out for me :huh:

im actually redesigning it in Yii, i already have a working copy of the project written

with raw PHP, Javascript, Mysql,

Wait a minute. I didnt mean to collect all DB to one table :)

Do you already have data model ( graphical ) for your entities? Yes, you have not simple DB structure, but try to model all your tables and you will see a lot of ralations to many tables that are hard to perceive.

I share a solution at that topic.