doqdcks3948 2013-08-09 10:05
浏览 42
已采纳

Yii使用PHP代码而不是gii工具生成模型文件

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条回答 默认 最新

  • douzhao2047 2013-08-09 10:57
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧