I wanted to extend/modify the class factory behavior to cope with CActiveDataProvider restrictions.
- What I want to do:
-
Show the contents of my table with CGridView.
-
Use CActiveDataProvider to provide the data.
-
Feed a generic class in CActiveDataProvider and parameters to specialise it.
- Background
-
My database has many tables that are dedicated to a (user,device). A user can have many devices and a device can have several users over its lifetime. So these tables get a specific name ‘devicelog_u1_d2’ where 1 is the userid and 2 the deviceid. There are some good reasons to do seperate tables.
-
These tables have the same structure, so I defined a generic class ‘Devicelog’ which can get parameters on instantiation (userid and deviceid). So I want to reuse the model.
- How I thought this would work:
- Use the model as an entry to the CActiveDataProvider which is instantiated like this:
$dataprovider = new CActiveDataProvider(‘Devicelog’,$config). I was hoping that I could pass parameters in $config for the ‘Devicelog’ class, but I can’t.
- In ‘Devicelog’ the ‘tableName’ method would simply return the right name for the table.
- What does not work.
-
Unfortunately parameters can not be passed to ‘Devicelog’ through the CActiveDataProvider instantiation.
-
I can not name ‘Devicelog’ something like ‘Devicelog_u1_d2’ and use that string upon instantiation to get the parameters. I looked at ‘autoload’ and ‘import’ in YiiBase.php. There may be some trick to preset the $alias as ‘imported’ but I think to get ‘Devicelog_XX’ Yii reverts to ‘autoload’. Anyway, analysis is pretty timeconsuming to know what would work, and it would only be a hack on the system (for as far as I know it righ now).
- So basically, it would be nice:
-
if we could define any ‘classname’ and have our implementation code behave as the factory for it. That would allow great flexibility;
-
if we could pass parameters to the model instantiation for the CActiveDataProvider.
In the mean time, it would be nice if somebody can make a nice suggestion on how to achieve what I want in an easy, clean way. I agree that I could extend the CActiveDataProvider class and adapt it in the required places. Unless there are some good suggestions, that is I guess what I will do. And of course, maybe yii can evolve to make this more flexible (but that is not for the short term).
Thanks for reading.
P.S.: Yii looked cool, and after going through the learning curve, it does look pretty much cool ;-).