Factory method for ActiveRecord

Hi there Yii folks,

Just following on from http://www.yiiframework.com/forum/index.php?/topic/13324-override-construct-in-activerecord

Where I was having problems setting an ActiveRecord getTable() method in the constructor.

I started the new post as what I need to do has been clarified by Mike (thanks!). Hope that is ok.

This would save a huge amount of model generation as I have about 30 tables all with identical structures but with

different names.

So far I recognize that php does not support constructor overloading , so I am trying to put extra info into the constructor through an array.


 


// just one variable  - an array 

class RefTable extends CActiveRecord { 

     private $_table; 

     public function __construct( $param = array('scenario'=>'insert' , 'table'=>'gender' ) )

     { 

       $this->_table = $param['table']; 

       parent::_construct( $param['scenario'] ); 

     }


     public function getTable()

     {

         return $this->_table; 

     }

}


 

// two variables 

class RefTable extends CActiveRecord { 

     private $_table; 

     public function __construct( $scenario = 'insert' , $table = 'gender'  )

     { 

       $this->_table = $table; 

       parent::_construct( $scenario ); 

     }


     public function getTable()

     {

         return $this->_table; 

     }


}




// hacked , it will never know... 

class RefTable extends CActiveRecord { 

     private $_table; 

     public function __construct( $scenario = 'gender' )

     { 

       $this->_table = $scenario; 

       parent::_construct( 'insert' ); 

     }


     public function getTable()

     {

         return $this->_table; 

     }


}


// and in actionSomeController 

$obj = new RefTable('gender'); 




None of it works. I am certain Yii has a simple and elegant solution. If I find out I will let you know.

If you have ideas please let me know ;) . Although the last solution is sloppy it should work but it isnt.

Problem is also that Yii seems to abort and crash silently!!! Beginning to wonder if there is a segfault upstream in

php apache module.

For ref I am using php 5.2.5 , apache 2.2.3 and Yii 1.1.4.r2429

Cheers,

May be if you have faced with such problem you need not to override ActiveRecord but change your database structure?

Hi there derelict ,

Thanks for the reply. It is noramlized (see link in post) so it is in a structure that conforms to relational standards. Essentially the structure is correct.

What I am trying to do is re use code. Sure I can generate 30 models but would I rather

use one model and bend it slightly on the fly. Sure who would’nt. Thanks anyway.

I think that using such universal AR you will have problems with relations, for example.

You say that Yii is crashing, so this is rather a sign that something’s wrong with your setup. Like you said i’d study the webserver logs and maybe increase loglevel. If you can, try the code on another server. It should never cause a crash.

The way AR works, one class instance per class name is generated on the call to $this->getMetaData() from the constructor. BTW you should add the model method like the documentation says

I believe it should be possible to make the first instance of ‘refTable’ work if a call to $this->refreshMetaData() is added to the factory method suggested by Mike.

(not tested)

/Tommy

Hi there,

I was going to add all the refs into one table and group them by another integer field. Thanks for the idea derilict. Might still do that as it seems more Yii’ish. But will try Tommy’s method too.

As for Yii crashing, it is definitely an apache child process , related to libphp5.so so I have set up a core dump configuration in httpd.conf and will run any core files through gdb. Maybe php does not like constructor overloading at all.

Anyway thanks for all the help, really enjoying Yii and grateful for a knowledgeable and friendly community.

Laters,

Spiro

I meant $record->refreshMetaData()

/Tommy