Yii使用php代码而不是gii工具生成模型文件 [英] Yii generate a model file using php code instead of gii tool

查看:133
本文介绍了Yii使用php代码而不是gii工具生成模型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下主题的实际问题:

我正在使用Yii框架创建一个社交网络,并且为每个用户生成一堆以用户ID开头的数据库表,因此一个数据库表看起来像:13_my_firends.

重点是,这样做无法使用gii工具生成特定模型,并且我确实需要为此表建立一些模型来避免内部联接,所以我想问题是这样的:/p>

->如何使用php或Yii代码创建模型文件,并在该代码中更改"public function relationship(){}"方法以添加我的关系?

我只是在考虑:是否有可能将关系添加到父母而不是儿子?我的意思是...我可以更改主数据库表(在我的情况下,女巫是sys_accounts-这个模型): ->在另一个方法中调用Relations()方法 ->制作关系数组(有点像联接)

所以基本上,这就像...仅在需要时才更改Relationship()方法.是否有可能?如果可以,怎么办?

解决方案

您可以自定义gii工具并使其动态生成模型,例如

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);

    }

仍然需要更多更新才能使其正常运行.但是根据您的要求,我阅读的内容似乎与您的需求不符.

您期望有很多东西可以改善您的关系,而Yii提供的东西似乎还需要更多东西,您应该了解模板知识. 我可以建议使用生成器工具,该工具可以帮助开发人员构建自己的模板源代码,然后您可以阅读数据库结构和架构等,并根据需要创建任何模型模板.

http://www.codesmithtools.com/product/generator

我已经在.NET和SQL SERVER中使用了此工具,但在PHP和MYSQL上也支持

http://community.codesmithtools.com/support_forums/f/3/p/11630/44588.aspx

Codesmith教程以编写模板

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?

解决方案

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

这篇关于Yii使用php代码而不是gii工具生成模型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆