xutongle

xutongle

这家伙有点懒,还没写个性签名!

  • 财富值660
  • 威望值60
  • 总积分1720

个人信息

  • 赞了说说
    右眼跳,我要去看一声!!
  • 赞了说说
    react & yii
  • 赞了说说
    昨天去一家餐馆吃饭,门口挂着的横幅令我到现在念念不忘:你的肚子,是我搞大的。。。
  • 赞了说说
    er
  • 2016-03-23 已签到
    连续签到1天,获得了5个金钱
  • 回复了 的回答

    你是不是希望有个->comment('xxx')方法啊 哈哈,好吧,这种非标准的语法不兼容其他数据库,所以yii没提供,只能自己按照你前一种写了,

    这个改框架应该可以,不过就一注释而已,犯不着的

  • 赞了回答

    看了下源码,最终会调用:yii\db\QueryBuilder的createTable方法

    public function createTable($table, $columns, $options = null)
        {
            $cols = [];
            foreach ($columns as $name => $type) {
                if (is_string($name)) {
                    $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type);
                } else {
                    $cols[] = "\t" . $type;
                }
            }
            $sql = "CREATE TABLE " . $this->db->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)";
    
            return $options === null ? $sql : $sql . ' ' . $options;
        }
    

    他举了个例子:

    $sql = $queryBuilder->createTable('user', [
        'id' => 'pk',
        'name' => 'string',
        'age' => 'integer',
    ]);
    

    也就是数组中的key会作为字段,value作为类型,然后得看这个方法getColumnType

    public function getColumnType($type)
        {
            if ($type instanceof ColumnSchemaBuilder) {
                $type = $type->__toString();
            }
    
            if (isset($this->typeMap[$type])) {
                return $this->typeMap[$type];
            } elseif (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) {
                if (isset($this->typeMap[$matches[1]])) {
                    return preg_replace('/\(.+\)/', '(' . $matches[2] . ')', $this->typeMap[$matches[1]]) . $matches[3];
                }
            } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) {
                if (isset($this->typeMap[$matches[1]])) {
                    return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type);
                }
            }
    
            return $type;
        }
    

    他首先判断传过来的类型是不是ColumnSchemaBuilder这个对象,如果不是,做一些正则判断,最终返回一个字符串,那反正最终就是构成一个合法的字段声明, 比如:

    `phone`  int(11) NULL DEFAULT NULL COMMENT '备注' ,
    

    那你可以试试,在构建的时候,直接这样做:

    'phone' => 'int(11) NULL DEFAULT NULL COMMENT "备注"'
    

    这样应该也是可行的。具体需要你去试试。

  • 赞了说说
    头像是狗头的都来点个赞!
  • 赞了回答

    我用的是sphinx搜索引擎,其实是三张表,索引到三个索引中,搜索的时候是三个索引一起被搜索,结果就是你看到的情况。

  • 2016-03-19 已签到
    连续签到2天,获得了10个金钱
经理 等级规则
1720/2000
资料完整度
40/100
用户活跃度
0/100

Ta的关注

6

Ta的粉丝

16

Ta的访客

109