2016-03-17 10:12:39 3459次浏览 5条回答 1 悬赏 100 金钱

之前的版本实现:

'phone' => Schema::TYPE_INTEGER . '(11) NOT NULL COMMENT "备注"'

2.0.7

'phone' =>$this->integer([11])->notNull()->?

最佳答案

  • javalzbin 发布于 2016-03-17 12:08 举报

    看了下源码,最终会调用: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 "备注"'
    

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

    1 条回复
    回复于 2016-03-17 21:26 回复

    嗯,成功执行 。发现这样做还可以避免遗忘sql基本操作呢

    觉得很赞
  • 回答于 2016-03-17 10:30 举报

    2.0.7不支持之前版本的实现了?是不能那样用了吗?

    1 条回复
    回复于 2016-03-17 21:20 回复

    向下兼容的,觉得新方法用起来更直观

  • 回答于 2016-03-17 10:59 举报

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

    3 条回复
    回复于 2016-03-17 21:21 回复

    嗯嗯,聪明如你

    回复于 2016-03-23 11:13 回复

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

    回复于 2018-06-06 16:55 回复

    我找这个方法,结果没有,哈哈

  • 回答于 2016-03-17 11:14 举报

    之前的方法还能接着用吧?

    1 条回复
    回复于 2016-03-17 21:21 回复

    可以的,向下兼容

  • 回答于 2016-04-21 16:54 举报

    不错,'username' => $this->string(32)->notNull()->unique()." COMMENT '管理员账号'",
    这样果然实现了 坐等2.0.8

    2 条回复
    回复于 2018-06-06 16:57 回复

    此法不错,但是还是希望有个选项或并联一个方法

    回复于 2019-01-24 20:40 回复

    'phone_number' => $this->string(11)->notNull()->unique()->comment('手机号') 现在可以这样了已经

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
杨淇
副总裁

杨淇 西南

注册时间:2015-09-25
最后登录:2022-04-12
在线时长:51小时48分
  • 粉丝12
  • 金钱5620
  • 威望10
  • 积分6230

热门问题