问题
I'm having real problems with the following topic:
I'm creating a social network using Yii framework and for each user I'm generating a bunch of DB tables that start with the user id, so a DB table looks like: 13_my_firends.
The point is that by doing this, I cannot generate a certain model using the gii tool, and I really need some models for this tables to make some relations to avoid inner joins, so I guess the problem is like this:
-> How can I create a model file using php or Yii code and also, within that code alter the "public function relation(){}" method to add my relations?
EDIT:
I was just thinking about this: Is there a possibility to add the relation to the parent and not the son? What I mean is... Can I alter the main DB table (witch in my case is sys_accounts - this one has a model) like: -> call the relations() method in another method -> make the relations array (kind of like a join)
So basically it's like... altering the relations() method only when I want to. Is it possible? And if so, how?
回答1:
You can customize gii tool and make it dynamically generate model such as
Yii::app()->getModule('ssgii')->SSGenModel($dynamictableName, $dynamicModelName);
public function SSGenModel($tableName, $modelName){
$_ccodeGen = $this->controllerMap;
$ccodeGen = new CCodeGenerator('', null);
$ccodeGen->codeModel = 'ssgii.generators.model.ModelCode';
$model=$ccodeGen->ssAutoGenModel($tableName, $modelName);
return ($model);
}
There still need some more updates to get it work. But from your requirement what I read, it seems not to be fit with your need.
You have expected much things to dynamic your relation, and it seems more further necessary things what Yii provides, you should have to comprehend around template knowledge. I can suggest the generator tool which help developer to build their own template source code, then you could read up your database structure and schema, etc and make any model template what you need.
http://www.codesmithtools.com/product/generator
I've used this tool in .NET and SQL SERVER, but it also supports on PHP and MYSQL
http://community.codesmithtools.com/support_forums/f/3/p/11630/44588.aspx
Codesmith Tutorial to write template
来源:https://stackoverflow.com/questions/18144254/yii-generate-a-model-file-using-php-code-instead-of-gii-tool